#include "stdio.h"
#include "string.h"
int palindrome(int n,char a[]) //是否回文
{
int i;
for (i = 0; i <= n / 2; i++)
{
if (a[i] != a[n - 1 - i])
return 0;
}
return 1;
}
void reverse(int n, char a[]) //反置
{
int i;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
int ctoi(char a) //字符转数字
{
if (a >= '0'&&a <= '9')
return a - '0';
else if (a >= 'a'&&a <= 'z')
return a - 'a' + 10;
else if (a >= 'A'&&a <= 'Z')
return a - 'A' + 10;
}
char itoc(int a) //数字转字符
{
char s[17] = "0123456789ABCDEF" ;
return s[a];
}
int main()
{
int N,count=0,len,i,flag=0,temp;
char M[100] = {0}, M1[100] = { 0 }, M2[100] = { 0 }; //M用来存放初始数组和迭代用
scanf("%d %s", &N, M);
while (count+1)
{
strcpy(M1, M);
reverse(strlen(M), M);
strcpy(M2, M);
memset(M, 0, sizeof(M));
if (count <= 30)
{
len = strlen(M1);
for (i = 0; i < len; i++)
{
temp = ctoi(M1[i]) + ctoi(M2[i]) + flag;
flag = temp / N;
temp = temp%N;
M[i] = itoc(temp);
}
if (flag != 0)
M[len] =itoc(flag);
count++;
if (palindrome(strlen(M), M) == 1)
{
printf("STEP=%d", count);
break;
}
reverse(strlen(M), M);
flag = 0;
}
else
{
printf("Impossible!");
break;
}
}
return 0;
}
0.0分
0 人评分