Vijos 1304 回文数 题解

版权声明:随便转载。 https://blog.csdn.net/zhang14369/article/details/78719194

题目

Vijos原题

思路

这就是一道大模拟!其中涉及一些高精度运算。
所需函数:

  1. 判断是不是回文数(judge)
  2. 高精度相加(myplus)

注意:

  • 字符串的运用
  • 进位
  • 逆序存储

我的源代码

//Vijos 1304
#include<iostream>
#include<cstdio>
typedef long long ll;

using namespace std;
inline int read(){
     int x=0,f=1;char ch=getchar();
     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
     return x*f;
}

const int maxn=10001;
string str;
int jz,n,a[maxn];
bool judge(int n){
    for(int i=1;i<=n/2;i++){
        if(a[i]!=a[n-i+1])return false;
    }
    return true;
}
int myplus(int n){
    int c[maxn]={0},s=1;
    for(int i=1;i<=n;i++){
        c[i]=a[i]+a[n-i+1]+c[i];
        c[i+1]+=c[i]/jz;
        c[i]%=jz;
    }
    if(c[n+1]!=0)n++;
    for(int i=n;i>=1;i--){
        a[s]=c[i];
        s++;
    }
    return n;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    cin>>jz>>str;
    n=str.size();
    for(int i=1;i<=n;i++){
        if('0'<=str[i-1]&&str[i-1]<='9'){
            a[i]=str[i-1]-'0';
        }
        else{
            a[i]=str[i-1]-55;
        }
    }
    for(int i=1;i<=30;i++){
        if(judge(n)){
            printf("STEP=%d",i-1);
            return 0;
        }
        n=myplus(n);
    }
    printf("Impossible!");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhang14369/article/details/78719194