POJ 1016
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 string str[16];
6
7 void process(string input, int index)
8 {
9 if(index == 16)
10 {
11 return;
12 }
13
14 int number[10] = {0};
15 for(int i=0; i<input.length(); i++)
16 {
17 number[input[i]-'0']++;
18 }
19 string temp;
20 for(int i=0; i<10; i++)
21 {
22 if(number[i] > 0)
23 {
24 if(number[i]<10)
25 {
26 temp.insert(temp.end(), number[i]+'0');
27 }
28 else
29 {
30 temp.insert(temp.end(), number[i]/10 + '0');
31 temp.insert(temp.end(), number[i]%10 + '0');
32 }
33
34 temp.insert(temp.end(), i+'0');
35 }
36 }
37 str[index] = temp;
38 process(temp, index+1);
39 }
40
41 int find_loop(int index)
42 {
43 for(int i=index+1; i<=15; i++)
44 {
45 if(str[index] == str[i])
46 {
47 return i-index;
48 }
49 }
50
51 return 0;
52 }
53
54 int main()
55 {
56 string input;
57
58 while(cin >> input)
59 {
60 if(input == "-1")
61 {
62 break;
63 }
64
65 str[0] = input;
66 process(input, 1);
67
68 bool after = false;
69 if(str[0] == str[1])
70 {
71 cout << input << " is self-inventorying" << endl;
72 }
73 else
74 {
75 for(int i=1; i<=15; i++)
76 {
77 if(str[i] == str[i-1])
78 {
79 after = true;
80 cout << input << " is self-inventorying after " << i-1 << " steps" << endl;
81 break;
82 }
83 }
84
85 int loop = 0;
86 if(!after)
87 {
88 for(int i=0; i<=15; i++)
89 {
90 loop = find_loop(i);
91 if(loop > 0)
92 {
93 cout << input << " enters an inventory loop of length "<< loop << endl;
94 break;
95 }
96 }
97 }
98
99 if(!after && loop ==0)
100 {
101 cout << input << " can not be classified after 15 iterations" << endl;
102 }
103 }
104 }
105
106 return 0;
107 }
2 #include <string>
3 using namespace std;
4
5 string str[16];
6
7 void process(string input, int index)
8 {
9 if(index == 16)
10 {
11 return;
12 }
13
14 int number[10] = {0};
15 for(int i=0; i<input.length(); i++)
16 {
17 number[input[i]-'0']++;
18 }
19 string temp;
20 for(int i=0; i<10; i++)
21 {
22 if(number[i] > 0)
23 {
24 if(number[i]<10)
25 {
26 temp.insert(temp.end(), number[i]+'0');
27 }
28 else
29 {
30 temp.insert(temp.end(), number[i]/10 + '0');
31 temp.insert(temp.end(), number[i]%10 + '0');
32 }
33
34 temp.insert(temp.end(), i+'0');
35 }
36 }
37 str[index] = temp;
38 process(temp, index+1);
39 }
40
41 int find_loop(int index)
42 {
43 for(int i=index+1; i<=15; i++)
44 {
45 if(str[index] == str[i])
46 {
47 return i-index;
48 }
49 }
50
51 return 0;
52 }
53
54 int main()
55 {
56 string input;
57
58 while(cin >> input)
59 {
60 if(input == "-1")
61 {
62 break;
63 }
64
65 str[0] = input;
66 process(input, 1);
67
68 bool after = false;
69 if(str[0] == str[1])
70 {
71 cout << input << " is self-inventorying" << endl;
72 }
73 else
74 {
75 for(int i=1; i<=15; i++)
76 {
77 if(str[i] == str[i-1])
78 {
79 after = true;
80 cout << input << " is self-inventorying after " << i-1 << " steps" << endl;
81 break;
82 }
83 }
84
85 int loop = 0;
86 if(!after)
87 {
88 for(int i=0; i<=15; i++)
89 {
90 loop = find_loop(i);
91 if(loop > 0)
92 {
93 cout << input << " enters an inventory loop of length "<< loop << endl;
94 break;
95 }
96 }
97 }
98
99 if(!after && loop ==0)
100 {
101 cout << input << " can not be classified after 15 iterations" << endl;
102 }
103 }
104 }
105
106 return 0;
107 }