|
推荐
楼主 |
发表于 2018-7-23 20:49:34
|
只看该作者
- #include<iostream>
- using namespace std;
- int N;
- string M;
- string rever(string s)
- {
- int i,l=s.size();
- string t="";
- for (i=0; i<=l-1; i++) t=s[i]+t;
- return t;
- }
- string add(string s,string t)
- {
- int x,j,i,l1=s.size(),l2=t.size();
- int x1,x2;
- if (l1<l2)
- {
- swap(s,t);
- swap(l1,l2);
- }
- for (i=1; i<=l1-l2; i++) t='0'+t;
- string ss="";j=0;
- for (i=l1-1; i>=0; i--)
- {
- if (s[i]<='9')x1=s[i]-'0';else x1=s[i]-'A'+10;
- if (t[i]<='9')x2=t[i]-'0';else x2=t[i]-'A'+10;
- x=x1+x2+j;
- if (x>=N)
- {
- x=x-N;
- j=1;
- }
- else j=0;
- if (x<=9)ss=char(x+'0')+ss;
- else ss=char(x-10+'A')+ss;
- }
- if (j==1) ss='1'+ss;
- return ss;
- }
- string trim(string s)
- {
- string t=s;
- while (t[0]=='0' && t.size()>1) t.erase(0,1);
- return t;
- }
- int main()
- {
- int i;
- cin>>N;
- cin>>M;
- for(i=0; i<=30; i++)
- {
- ///cout<<M<<endl;
- string sm=rever(M);
- /// cout<<sm<<endl;
- if (M==sm) break;
- else
- {
- M=add(M,sm);
- M=trim(M);
- }
- }
- if (i<=30) cout<<"STEP="<<i;
- else cout<<"Impossible!";
- return 0;
- }
复制代码 |
|