C++-POJ1016-Numbers That Count

无语,15步产生16个数,植树原理啊!

大水题,居然wa了好几次,唉,自己的问题。

略略略,就要封装成结构体,略略略。

 1 #include <set>
 2 #include <map>
 3 #include <cmath>
 4 #include <queue>
 5 #include <vector>
 6 #include <cstdio>
 7 #include <cstdlib>
 8 #include <cstring>
 9 #include <iostream>
10 #include <algorithm>
11 using namespace std;
12 struct String {
13     char s[81];
14     int len_s,len_b;
15     int a[10],b[50];
16     void init() {
17         memset(s,0,sizeof(s));
18         memset(a,0,sizeof(a));
19         memset(b,0,sizeof(b));
20         len_s=len_b=0;
21     }
22     bool read() {
23         scanf("%s",s+1);
24         len_s=strlen(s+1);
25         if(s[1]=='-')return false;
26         return true;
27     }
28     void calc() {
29         for(int i=1; i<=len_s; i++)a[s[i]-'0']++;
30         for(int i=0; i<=9; i++)
31             if(a[i]) {
32                 if(a[i]<10){
33                     b[++len_b]=a[i];
34                     b[++len_b]=i;
35                 }
36                 else {
37                     b[++len_b]=a[i]/10;
38                     b[++len_b]=a[i]%10;
39                     b[++len_b]=i;
40                 }
41             }
42     }
43     void get(String A) {
44         len_s=A.len_b;
45         for(int i=1; i<=len_s; i++)
46             s[i]=A.b[i]+'0';
47     }
48     void print_s() {for(int i=1; i<=len_s; i++)cout<<s[i];}
49 };
50 bool comp(String A,String B) {
51     if(A.len_s!=B.len_s)return false;
52     for(int i=1; i<=A.len_b; i++)if(A.s[i]!=B.s[i])return false;
53     return true;
54 }
55 bool self(String A) {
56     if(A.len_s!=A.len_b)return false;
57     for(int i=1; i<=A.len_s; i++)if(A.s[i]-'0'!=A.b[i])return false;
58     return true;
59 }
60 String a[20],A;
61 int main() {
62     while(A.read()) {
63         A.calc(),a[1]=A;
64         for(int i=2; i<=16; i++) a[i].get(a[i-1]),a[i].calc();
65         int ans,flag=0;
66         for(int i=1; i<=15; i++) {
67             if(flag)break;
68             if(self(a[i]))
69                 {flag=1;ans=i-1;break;}
70             for(int j=i+1; j<=16; j++)
71                 if(comp(a[i],a[j])) 
72                     {ans=j-i;flag=2;break;}
73         }
74         A.print_s();
75         if(flag==1)
76             if(ans==0)cout<<" is self-inventorying"<<endl;
77             else cout<<" is self-inventorying after "<<ans<<" steps"<<endl;
78         else if(flag==2)cout<<" enters an inventory loop of length "<<ans<<endl;
79         else cout<<" can not be classified after 15 iterations"<<endl;
80         A.init();for(int i=1;i<=16;i++)a[i].init();
81     }
82     return 0;
83 }

 

posted @ 2020-02-06 13:26  墨鳌  阅读(106)  评论(0编辑  收藏  举报