POJ1350解题报告

 
Cabric Number Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8048 Accepted: 2460

Description

If we input a number formed by 4 digits and these digits are not all of one same value, then it obeys the following law. Let us operate the number in the following way:
(1) Arrange the digits in the way from bigger to smaller, such that it forms the biggest number that could be made from these 4 digits;
(2) Arrange the digits in the way from smaller to bigger, such that it forms the smallest number that could be made from these 4 digits (If there is 0 among these 4 digits, the number obtained may be less than four digits);
(3) Find the difference of these two numbers that is a new four digital number.
Repeat the above process, we can finally always get the result 6174 or 0.
Please write the program to realize the above algorithm.

Input

Each case is a line of an integer.-1 denotes the end of input.

Output

If the integer is formed exactly by 4 digits and these digits are not all of one same value, then output from the program should show the procedure for finding this number and the number of repetition times. Otherwise output "No!!".

Sample Input

5364
2221
4444
-1

Sample Output

N=5364:
6543-3456=3087
8730-378=8352
8532-2358=6174
Ok!! 3 times
N=2221:
2221-1222=999
999-999=0
Ok!! 2 times
N=4444:
No!!
题意比较简单:就是输入一个数(各个数位不全相同,且数字的长度为4),将它的各位从大到小排得的数maxnum和各位从小到大排的数minnum相减,反复循环直至值为0或者6147。且如果开头有0那么0不参与排序。
陷阱是,如果输入的数小于4位或者大于4位都输出No!!如果忽略了这一点就会OLE。
#include<stdio.h>
#include<string.h>
int index;
void print(char *num)
{
   int tag=1;
   int i;
   for(i=0;i<4;i++)
   {
      if(tag&&num[i]=='0')
      {}
      else
      {
         printf("%c",num[i]);
         tag=0;
      }
   } 
}
int getNum(char *num)
{
   return 1000*(num[0]-'0')+100*(num[1]-'0')+10*(num[2]-'0')+num[3]-'0';
}
int trans(char *num)
{
   char maxnum[5];
   char minnum[5];
   int i;
   int j;
   int now;
   int result;
   int result1;
   char temp;
   strcpy(maxnum,num);
   strcpy(minnum,num);
   
   //不能全都有相同的数字组成 
   
   result=1;
   j=num[0];
   for(i=1;i<4;i++)
   {
      if(num[i]!=j)
      {
         result=0;
         break;
      }
   }
   if(result&&(index==0))
   {
     return -1;
   }
   //冒泡排序
   i=0;
   for(i=0;i<4;i++)
   {
      if(num[i]!='0')
      {
         break;
      }
   }
   now=i;
   //printf("now=%d",now);
   //0不参与排序 
   for(i=now;i<3;i++)
   {
      for(j=now;j<3-i;j++)
      {
          if(minnum[j]>minnum[j+1])
          {
               temp=minnum[j];
               minnum[j]=minnum[j+1];
               minnum[j+1]=temp;
          }
           if(maxnum[j]<maxnum[j+1])
          {
               temp=maxnum[j];
               maxnum[j]=maxnum[j+1];
               maxnum[j+1]=temp;
          } 
      }
   }
   //冒泡排序结束后
   print(maxnum);
   printf("-");
   print(minnum);
   result=getNum(maxnum)-getNum(minnum);
   printf("=%d\n",result);
   j=1000;
   result1=result;
   for(i=0;i<4;i++)
   {
       num[i]=result1/j+'0';
       result1%=j;
       j/=10;
   } 
   index++;
   if(result==0||result==6174)
   {
      return 0;
   }
   return 1;
}
int main()
{
 //输入的是4位数字 ,最后一位位\0 
 char num[5];
 int tag;
 scanf("%s",num);
 while(strcmp(num,"-1")!=0)
 {
     printf("N=%s:\n",num);
     index=0;
     if(strlen(num)<4||strlen(num)>4)
     {
         printf("No!!\n");
     }
     else
     {
          while((tag=trans(num))==1);
          if(tag==-1&&index==0)
          {
             printf("No!!\n");
          }
          else
          {
              printf("Ok!! %d times\n",index); 
          }
     }
     scanf("%s",num);
 }
 
 system("pause");
 return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值