看不懂你的做法,不过感觉用整型来做不安全
#include
#include
using namespace std;
char table[] = "0123456789ABCDEF";
class BigNum
{
public:
BigNum(char *s, int m)
{
strcpy(num,s);
M = m;
}
BigNum convert()
{
BigNum temp(num,M);
int len = strlen(num);
for(int i=0;i
return temp;
}
BigNum operator+(const BigNum& a)
{
assert(M==a.M);
BigNum temp(num,M);
int len = strlen(num);
temp.num[0] = 0;
for(int i=0;i
if(a.num[i]>='0' && a.num[i]<='9') temp.num[i]+=a.num[i]-'0';
else temp.num[i]+=a.num[i]-'A'+10;
if(num[i]>='0' && num[i]<='9') temp.num[i]+=num[i]-'0';
else temp.num[i]+=num[i]-'A'+10;
temp.num[i+1] = 0;
if(temp.num[i]>=M)
{
temp.num[i+1] = 1;
temp.num[i]-=M;
}
temp.num[i] = table[temp.num[i]];
}
if(temp.num[len])
{
temp.num[len] = '1';
temp.num[len+1] = 0;
}
return temp;
}
bool check()
{
int len = strlen(num);
for(int i=0;i
if(num[i]!=num[len-1-i])
return false;
}
return true;
}
// void print()
// {
// cout << num;
// }
private:
char num[1000];
int M;
};
int main()
{
int M;
char s[1000];
while(cin >> M >> s)
{
BigNum temp(s,M);
BigNum temp2(s,M);
int step = 0;
while(step <= 30 && !temp.check())
{
temp2 = temp.convert();
//temp.print();
//cout << " + ";
//temp2.print();
//cout << " = ";
temp = temp + temp2;
//temp.print();
//cout << endl;
step++;
}
if(step>30)
{
cout << "Impossible!" << endl;
}
else
{
cout << "STEP=" << step << endl;
}
}
}