0分求调!

P1015 [NOIP1999 普及组] 回文数

zhukexv @ 2024-04-20 00:40:37

#include<bits/stdc++.h>
using namespace std;
int n,a[120],b[120],ans;
string m;
int fz(int j){
    for(int i=0;i<=j;i++){
        b[j-i]=a[i];
    }
    int z=119;
    while(b[z]==0) z--;
    return z;
}
int add(int n){
    for(int i=0;i<120;i++){
        a[i]+=b[i];
        a[i+1]+=(a[i]/n);
        a[i]%=n;
    }
}

int pd(){
    int len=119,f=1;
    while(a[len]==0) len--;
    for(int i=0;i<len;i++){
        if(a[i]!=a[len-i]){
            f=0;
            break;
        }
    }
    if(f) return 1;
    else return 0;
}
int one(int n,int j){
    while(1){
        ans++;
        j=fz(j);
        add(n);
        if(pd()==1){
            break;
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=0;i<m.size();i++){
        a[i]=m[i]-'0';
    }
    one(n,m.size()-1);
    if(ans<=30) printf("STEP=%d",ans);
    else cout<<"Impossible!";
    return 0;
}

by like_1999 @ 2024-04-21 13:11:13

@zhukexv 菜就多练


by like_1999 @ 2024-04-21 13:13:35

#include<bits/stdc++.h>
using namespace std;
string m;
int n;
int m_long;
int p[20000000], q[20000000], l;
void zhuan() {
    int j = 0;
    for (int i = m_long - 1 ; i >= 0 ; i--) {
        if (m[i] >= '0' && m[i] <= '9') {
            p[j] = m[i] - '0';
        } else {
            p[j] = m[i] - 'A' + 10;
        }
        j++;
    }
}
bool huiwen(int a[]) {
    int i = 0;
    int j = m_long - 1;
    while (i <= (m_long / 2)) {
        if (a[i] != a[j]) return false;
        i++;
        j--;
    }
    return true;
}
void turn(int a[]) {
    int j = 0;
    for (int i = m_long - 1 ; i >= 0 ; i--) {
        q[j] = a[i];
        j++;
    }
}
void add(int a[] , int b[]) {
    for (int i = 0 ; i < m_long ; i++) {
        a[i] += b[i];
        a[i + 1] += a[i]/n;
        a[i] %= n;
    }
    if (a[m_long]) {
        m_long++;
    }
}
int main() {
    cin >> n >> m;
    m_long = m.size();
    zhuan();
    while (!huiwen(p)) {
        turn (p);
        add(p , q);
        l++;
        if (l > 30) {
            cout << "Impossible!";
            return 0;
        }
    }
    cout << "STEP=" << l;
    return 0;
}

by xpg007 @ 2024-05-02 22:18:06

@chenzixuan49

冒昧的问一下,你不知道string吗?


|