vijos p1304 回文数
N进制下的加法
2/10/16进制下,char到int的转换
#include<iostream>
#include<string>
using namespace std;
string str;
int step = 1;
int n;
int m;
int a[10001];
int b[10001];
void doit() {
int JinWei = 0;
for (int i = 0; i < m; i++)
{
a[i] += b[i] + JinWei;
JinWei = a[i] / n;
a[i] %= n;
}
if (JinWei > 0)
a[m++] = JinWei;
for (int i = 0; i < m; i++)
b[i] = a[m - i - 1];
step++;
}
bool judge()
{
int flag = true;
for (int k = 0; k < m; k++)
{
if (a[k] != b[k])
flag = false;
}
return flag;
}
int main() {
cin >> n >> str;
m = str.length();
for (int i = 0; i < m; i++)
{
if (str[i] >= 65)
{
a[i] = str[i] - 55;
}
else
{
a[i] = str[i] - '0';
}
}
for (int i = 0; i < m; i++)
{
b[i] = a[m - i - 1];
}
while (step <= 30)
{
if (judge())
{
cout << "STEP=" << step - 1;
system("pause");
return 0;
}
doit();
}
cout << "Impossible!";
system("pause");
return 0;
}