//本来想练练itoa,,结果oj不支持,,,然后就自己写了个itoa1的函数,然后引用那儿遇到了问题,需要写成char (&a)[15] 必须&与数组名a用括号括起来,应该只能写成这种样子。 然后,数组变量不引用也可以通过形参修改实参值。通过形参 可在子函数中改变形参所指向的数组中的内容,而实参对应的数组中的内容会随之改变。因为这是通过地址对地址中的内容进行更改。
#include<iostream>
#include<cstdio>
//#include<cctype>
#include<cstring>
#include<cstdlib>
using namespace std;
int n;
char a[15];
void itoa1(int _Val, char (&a)[15], int _Radix){
int cnt = 0;
while(_Val){
a[cnt++] = _Val%_Radix+'0';
_Val /= _Radix;
}
a[cnt] = '\0';
// cout << _DstBuf << endl;
}
int main()
{
while(cin >> n && n){
int b[16], cnt = 0;
for(int i=2; i<17; i++){
memset(a, 0, sizeof(a));
a[0] = '\0';
itoa1(n, a, i);
/*
int cont = 0, tmp = n;
while(tmp){
a[cont++] = tmp%i+'0';
tmp /= i;
}
// a[cnt] = '\0';
// int lena = cont, j, k;*/
int lena = strlen(a), j, k;
// cout << i << " : " << a << " " << cont << endl;
for(j=0, k=lena-1; j < k; j++, k--){//可能有一种情况是11(16)
if(a[j] != a[k]){
break;
}
}
if(j == k || (k+1) == j){//是回文数
b[cnt++] = i;
}
}
if(cnt == 0){
printf("Number %d is not palindrom\n", n);
continue;
}
printf("Number %d is palindrom in basis", n);
for(int i=0; i<cnt; i++){
printf(" %d", b[i]);
}
printf("\n");
}
return 0;
}