Palindrom Numbers(水题-回文+进制)

题目链接:http://exam.upc.edu.cn/problem.php?id=1243

这个题用到了回文加进制转化。其实做得多了就觉得很水。。。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n),n){
        int ans[16]={0},cnt=0;
        for(int i=2;i<=16;i++){
            string s;
            int tmp=n;
            while(tmp){
                s+=((tmp%i)+'0');
                tmp/=i;
            }
            string t(s);
            reverse(t.begin(),t.end());
            //cout<<s<<"   "<<t<<endl;
            if(t==s){
                ans[cnt++]=i;
            }
        }
        if(cnt){
            printf("Number %d is palindrom in basis ",n);
            for(int i=0;i<cnt;i++){
                printf("%d%c",ans[i],i==cnt-1?'\n':' ');
            }
        }else{
            printf("Number %d is not a palindrom\n",n);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z_sea/article/details/80487194