pku 1016 Numbers That Count

来源:互联网 发布:web编程软件手机 编辑:程序博客网 时间:2024/04/28 19:20

http://acm.pku.edu.cn/JudgeOnline/problem?id=1016
判断一个数结果变换后是否是self_number 或者 loop_number 或者经过15次变换后三名数都不是;变换规则是第二个数是第一个数的数字以及其出现的次数组合而成的。
#include <stdio.h>
#include <string.h>

char str[17][90];

int main()
{
 int i = 0,length,j,k,l,m,a,n,flag;
 scanf("%s",str[0]);
 while(strcmp(str[0],"-1") != 0)
 {
  j = 0;
  length = strlen(str[0]);
  while(j <= 15)
  {
   n = 0;
   for(l = 0;l < 10;l ++)
   { 
    length = strlen(str[j]);
    k = 0;
    for(m = 0;m < length;m ++)
    {
     if(l+'0' == str[j][m])
      k ++;
    }
    if(k == 0)
     continue;
    else
    {
     if(k >= 10)
     {
     str[j+1][n] = k/10+'0';
     str[j+1][n+1] = k%10+'0';
     str[j+1][n+2] = l+'0';
     n = n+3;
     }
     else
     {
      str[j+1][n] = k+'0';
      str[j+1][n+1] = l+'0';
      n = n+2;
     }
    }
   }
   str[j+1][n]='/0';
   j ++;
   if( strcmp(str[j],str[j-1]) == 0)
   {
    if(j-1 == 0)
    {
     printf("%s is self-inventorying/n",str[0]);
     break;
    }
    else
    {
     printf("%s is self-inventorying after %d steps/n",str[0],j-1);
     break;
    }
   }
   else
   {
    flag = 0;
    for(a = 0;a <= j-1;a ++)
    {
     if(strcmp(str[j],str[a]) == 0 && a!= j-1)
     {
      flag = 1;
      printf("%s enters an inventory loop of length %d/n",str[0],j-a);
      break;
     }
    }
    if(flag == 1)
     break;
   }
   if(j >= 15)
   {
    printf("%s can not be classified after 15 iterations/n",str[0]);
    break;
   }
  }
  scanf("%s",str[0]);
 }
 return 0 ;
}

/*
22
31123314
314213241519
21221314
111222234459
-1

 

22 is self-inventorying
31123314 is self-inventorying
314213241519 enters an inventory loop of length 2
21221314 is self-inventorying after 2 steps
111222234459 enters an inventory loop of length 2
*/

原创粉丝点击