回文数猜想(与6174问题很像)

#include<stdio.h>
#include<string.h>
char s[20];
int is_prime(int x)
{
    int n;
    sprintf(s,"%d",x);
    n=strlen(s);
    for(int i=0;i<=n/2;i++)
        if(s[i]!=s[n-i-1])
        return 0;
    return 1;
}
int get_next(int x)
{
    int n,y;
    sprintf(s,"%d",x);
    n=strlen(s);
    for(int i=0;i<n/2;i++)
    {
        char t=s[i];s[i]=s[n-i-1];s[n-i-1]=t;
    }
    sscanf(s,"%d",&y);
    return x+y;
}
int num[2000];
int main()
{
    int cnt;
    while(scanf("%d",&num[0])!=EOF)
    {
        int first=1;
        cnt=1;
        for(;;)
        {
            int found=0,i,j;
            num[cnt]=get_next(num[cnt-1]);
             for(i=1;i<=cnt;i++)
                {
                    if(is_prime(num[i]))
                   {
                       found=1;
                       printf("%d\n",i);
                       break;
                   }

                }
                if(found)
                {
                     for(j=0;j<=i;j++)
                       {
                           if(first)
                             first=0;
                          else
                          printf("--->");
                         printf("%d",num[j]);
                       }
                      break;
                }

            cnt++;
        }
        printf("\n");

    }
    return 0;
}
posted @ 2013-07-27 10:38  清风旋叶  阅读(188)  评论(0编辑  收藏  举报