POJ 1047 Round and Round We Go

 1 #include<stdio.h> 
 2 #include <string.h>
 3 #define MAX_LEN 60
 4 
 5 unsigned aNum[MAX_LEN+10];//存放倒过来的大数,从低位开始相乘
 6 unsigned aRes[MAX_LEN+10];//存放相乘后的大数
 7 unsigned aFlag[MAX_LEN+10];// 看是否匹配
 8 
 9 int main()
10 {
11     char sNum[MAX_LEN+10];
12     int nLen;
13     int i,j,k;
14     while (~scanf("%s",&sNum))//刚开始一直Output Limit Exceeded,提交了好几次,原来是这里不加~会死循环,但换成C++的cin可以,它包括ctrl+z结束符 
15     {
16         int f = 1;
17         nLen = strlen(sNum);
18         j = 0;
19         for (i = nLen-1; i >= 0; i--)//将大数倒过来 
20             aNum[j++] = sNum[i] - '0';
21         for (i = 2; i <= nLen; i++)//乘以2->n
22         {
23             memset(aRes, 0, sizeof(aRes));
24             for (j=0;j<=nLen-1;j++)//相乘
25                 aRes[j] = aNum[j] * i;
26             for (j = 0; j <=nLen-1; j++)//统一进位
27             {
28                 if (aRes[j] >= 10)
29                 {
30                     aRes[j+1] += aRes[j] / 10;
31                     aRes[j] %= 10;
32                 }
33             }
34             memset(aFlag, 0, sizeof(aFlag));
35             for (j = 0; j < nLen; j++)//相乘之后的大数与原来的数进行匹配,看是否匹配 
36             {
37                 unsigned tmp = aRes[j];
38                 k = 0;
39                 while (k < nLen)
40                 {
41                     if (aNum[k]==tmp && aFlag[k]==0)//如果对应匹配,而且当前位未曾匹配过,此过程是从原来的大数里从k=0,k++,对应查找相乘得到的大数, 
42                     {
43                         aFlag[k] = 1;//标记,表示已经匹配过
44                         break;
45                     }
46                     k++;
47                 }
48                 if (k >= nLen)//表示未匹配成功
49                 {
50                     f = 0;
51                     break;
52                 }
53             }
54             if (f == 0)
55             {
56                 printf("%s is not cyclic\n",sNum);
57                 break;
58             }
59         }
60         if (f == 1)
61              printf("%s is cyclic\n",sNum);
62     }
63     return 0;
64 }

posted on 2012-08-04 11:34  mycapple  阅读(238)  评论(0编辑  收藏  举报

导航