非常简单的题,没什么好说的.唯一值得一提的就是在判断是否是回文的时候只需遍历一半的元素即可,稍微提高一点性能.
#include<iostream> using namespace std; int main(void) { int n,i,j,flag; while(cin>>n&&n) { int base[17] = {0}; int c[30]; for(i=2;i<=16;i++) { int m = n; int len = 0; while(m) { //获取十进制数化为对应进制数的每一位 c[len++] = m%i; m/=i; } //判断是否是回文数 flag = 1; for(j=0;j<len/2;j++) { if(c[j]!=c[len-j-1]) flag = 0; } if(flag) base[i] = 1; } flag = 0; for(i=0;i<=16;i++) { if(base[i]==1) flag = 1; } if(!flag) cout<<"Number "<<n<<" is not a palindrom"<<endl; else { cout<<"Number "<<n<<" is palindrom in basis"; for(i=0;i<=16;i++) { if(base[i]==1) cout<<" "<<i; } cout<<endl; } } return 0; }
时间: 2024-11-07 09:49:15