ZOJ 1078-Palindrom Numbers

来源:互联网 发布:清理adobe软件 编辑:程序博客网 时间:2024/03/28 23:37

不难的一道水题,但是第一次wa了,因为没考虑到字符串末尾要加‘\0’

#include<iostream>#include<string.h>using namespace std;char s1[50],s2[50];int j;int fun(){    int i,k=0;    for(i=j-1;i>=0;i--)        s2[k++]=s1[i];    s2[k]='\0';    if(strcmp(s1,s2)==0)        return 1;    else        return 0;}int main(){    int n;    while(cin>>n)    {        if(n==0)            return 0;        int i,k=0,t;        int a[20];        for(i=2;i<=16;i++)        {            t=n;            j=0;            while(t>0)            {                s1[j++]=t%i;                t=t/i;            }            s1[j]='\0';            if(fun()==1)            {                a[k]=i;                k++;            }        }        if(k==0)            cout<<"Number "<<n<<" is not a palindrom"<<endl;        else        {            cout<<"Number "<<n<<" is palindrom in basis";            for(i=0;i<k;i++)                cout<<" "<<a[i];            cout<<endl;        }    }    return 0;}

0 0
原创粉丝点击