代码改变世界

6174问题

2015-05-30 15:00  HaodongGUO  阅读(113)  评论(0编辑  收藏  举报
#include<stdio.h>
#include<string.h>



int main()
{
    int num[10],found,count=1,n,i;
    int get_next(int x);
    scanf("%d",&num[0]);
    printf("%d",num[0]);
    while(1)
    {
        num[count]=get_next(num[count-1]);
        printf("-> %d",num[count]);
        int found=0;
        for(i=0;i<count;i++)
        {
            if(num[i]==num[count])
            {
                found=1;
                break;
            }
        }
        if(found)
        {
            break;
        }
        count++;
    }
    printf("\n");
    
    
    
    
    return 0;
 } 
 
 
 int get_next(int x)
 {
     char s[10];
     
     int i,j,a,b,n;
     n=strlen(s);
     sprintf(s,"%d",x);
     for(i=0;i<n;i++)
     {
         for(j=i+1;j<n;j++)
         {
             if(s[i]>s[j])
             {
                 char t= s[i];
                 s[i]=s[j];
                 s[j]=s[i];
             }
         }
     }
     scanf(s,"%d",&b);
     for(i=0;i<n/2;i++)
     {
         char  t = s[i];
         s[i]=s[n-1-i];
         s[n-1-i]=s[i];
     }
     sscanf(s,"%d",&a);
     return a-b;
 }