1. 程式人生 > 實用技巧 >P1015 迴文數

P1015 迴文數

題目如下:

思路:

1.題目的考點比較綜合【值得做的一個題目非常好!!!】

有進位制轉換,判斷迴文數,大數字的加減

程式碼如下:

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int p;
char y[6]={'A','B','C','D','E','F'};
bool is(string &n1){
    for(int i=0,j=n1.size()-1;i<j;i++,j--)
    {
        if(n1[i]!=n1[j])
        
return false; } return true; } void add(string &n1,string &n2,int &p){ int jinwei=0; for(int i=0;i<n1.size();i++) { int k=n1[i]+n2[i]-'0'-'0'+jinwei; jinwei=k/p; n1[i]=k%p+'0'; if(n1[i]>='10'&&n1[i]<='15') n1[i]=y[n1[i]-'
10']; } if(jinwei>0) n1+=jinwei+'0'; } int main(){ int count=0; string n1,n2; cin>>p>>n1;//p進位制的n1 for(int i=0;i<n1.size();i++){ if(n1[i]>='A'&&n1[i]<='F') n1[i]=n1[i]-'A'+10+'0'; } for(int c=0;c<30&&!is(n1);c++){ n2
=n1; reverse(n1.begin(),n1.end()); add(n1,n2,p); reverse(n1.begin(),n1.end()) ; count++; } if(is(n1)) cout<<"STEP="<<count<<endl; else cout<<"Impossible!"; return 0; } //2 //101111