25分求助 ACon#3

P1015 [NOIP1999 普及组] 回文数

wangzixiao12345 @ 2023-11-07 21:40:39

#include<bits/stdc++.h>
using namespace std;
int n;
string gaojing(string s1,string s2){
    int a[105],b[105],c[105];
    int lena=s1.size(),lenb=s2.size();
    int lenc=max(lena,lenb);
    for(int i=1;i<=lena;i++){
        a[i]=s1[lena-i]-'0';
    }   
    for(int i=1;i<=lenb;i++){
        b[i]=s2[lenb-i]-'0'; 
    }
    int m=0;
    for(int i=1;i<=lenc;i++){
        c[i]=(m+a[i]+b[i])%10;
        m=(m+a[i]+b[i])/10;
    }
    if(m){
        lenc++;
        c[lenc]=1;
    }
    for(int i=lenc;i>=1;i--){
        if(c[i]==0){
            lenc--;
        }else{
            break;
        }
    }
    string ans;
    for(int i=lenc;i>=1;i--){
        ans+=char(c[i]+48);
    }
    return ans;
}

int main(){
    string s,t;
    cin>>n>>s;
    for(int i=0;i<s.length();i++){
        if(s[i]<='Z'&&s[i]>='A'){
            s[i]=s[i]-'A'+'0'+10;
        }
    }
    t=s;
    reverse(t.begin(),t.end());
    if(s==t){
        cout<<"STEP=0";
        return 0;
    }
    int step=0;
    while(step<=30){
        step++;
        s=gaojing(s,t);
        t=s;
        reverse(t.begin(),t.end());
        if(t==s){
            cout<<"STEP="<<step;
            return 0;
        }
    }
    cout<<"Impossible!";
    return 0;
}

by DBL_MAX @ 2023-11-19 19:09:46

用得着写这么多吗?虽然我的压过行,但展开了也比你的少啊```cpp

include <bits/stdc++.h>

using namespace std; const int N = 105; int a[N], b[N]; int n, len; string m;

bool check() { for (int i = 0; i < len; ++i) if (a[i] != a[len - i - 1]) return false; return true; }

void ps() { for (int i = 0; i < len; ++i) b[i] = a[len - i - 1]; for (int i = 0; i < len; ++i) a[i] += b[i]; for (int i = 0; i < len; ++i) { a[i + 1] += a[i] / n; a[i] %= n; } if (a[len]) ++len; }

int main() { scanf("%d", &n); cin >> m; len = m.size(); for (int i = 0; i < len; ++i) if (m[i] >= '0' && m[i] <= '9') a[i] = m[len - 1 - i] - '0'; else a[i] = m[len - 1 - i] - 'A' + 10; int step; for (step = 1; step <= 30; ++step) { ps(); if (check()) break; } if (step <= 30) cout << "STEP=" << step << endl; else cout << "Impossible!" << endl; return 0; }


by DBL_MAX @ 2023-11-19 19:20:33

对不起,排版乱了。

#include <bits/stdc++.h>
using namespace std;
int a[105], b[105], n, len,step;
string m;
bool check() {
    for (int i = 0; i < len; ++i)
        if (a[i] != a[len - i - 1])
            return false;
    return true;
}
void ps() {
    for (int i = 0; i < len; ++i)b[i] = a[len - i - 1];
    for (int i = 0; i < len; ++i)a[i] += b[i];
    for (int i = 0; i < len; ++i) {a[i + 1] += a[i] / n;a[i] %= n;}
    if (a[len])++len;
}
int main() {
    scanf("%d", &n);
    cin >> m;
    len = m.size();
    for (int i = 0; i < len; ++i)
        if (m[i] >= '0' && m[i] <= '9')a[i] = m[len - 1 - i] - '0';
        else a[i] = m[len - 1 - i] - 'A' + 10;
    for (step = 1; step <= 30; ++step) {ps();if (check()){break;}}
    if (step <= 30)cout << "STEP=" << step << endl;
    else cout << "Impossible!" << endl;
    return 0;
}

|