A Za, A Za, Fighting...

坚信:勤能补拙

PKU 1016 Numbers That Count

问题:
http://poj.org/problem?id=1016

思路:
纯模拟,需要细心...

代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define NUM 10 /* 0..9 */
 5 #define UP 15 
 6 #define INPUT_LEN 81
 7 #define MAX_LEN 31
 8 char input[INPUT_LEN], inv[UP][MAX_LEN];
 9 int count[NUM]; 
10 
11 void
12 generate(char *src, char *dst)
13 {
14     int i, index, len = strlen(src);
15     memset(count, 0sizeof(count));
16     for(i=0; i<len; i++)
17         ++count[src[i]-'0'];
18 
19     index = 0;
20     for(i=0; i<NUM; i++) {
21         if(count[i] > 0) {
22             if(count[i] >= 10) {
23                 dst[index++= (count[i]/10)+'0';
24                 dst[index++= (count[i]%10)+'0';
25             } else {
26                 dst[index++= count[i]+'0';
27             }
28             dst[index++= i+'0';
29         }
30     }
31 }
32 
33 void
34 solve()
35 {
36     int i, index = 0;
37     generate(input, inv[index]);
38     if(strcmp(input, inv[index]) == 0) {
39         printf("%s is self-inventorying\n", input);
40         return;
41     }
42     ++index;
43     while(index < UP) {
44         generate(inv[index-1], inv[index]);
45         if(strcmp(inv[index], inv[index-1]) == 0) {
46             printf("%s is self-inventorying after %d steps\n", input, index);
47             return;
48         }
49         for(i=index-2; i>=0; i--)
50             if(strcmp(inv[index], inv[i]) == 0) {
51                 printf("%s enters an inventory loop of length %d\n", input, index-i);
52                 return;
53             }
54         if(index >= 1)
55             if(strcmp(inv[index], input) == 0) {
56                 printf("%s enters an inventory loop of length %d\n", input, index+1);
57                 return;
58             }
59         ++index;
60     }
61     printf("%s can not be classified after 15 iterations\n", input);
62 }
63 
64 int
65 main(int argc, char **argv)
66 {
67     while(scanf("%s", input) != EOF) {
68         if(strcmp(input, "-1"== 0)
69             break;
70         memset(inv, 0sizeof(inv));
71         solve();
72     }
73 }

posted on 2010-10-31 20:19 simplyzhao 阅读(179) 评论(0)  编辑 收藏 引用 所属分类: G_其他


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2011年7月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜