首页 > 代码库 > Poj 1016

Poj 1016

传送门:http://poj.org/problem?id=1016

数字字符串的压缩,比较

注意指针的使用,注意细节

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 void inventory(char *s,char *d){
 6     int n[10]={0}; 
 7     for(int i=0;s[i];i++){
 8         n[s[i]-0]++;
 9     }
10     int j=0;
11     for(int i=0;i<10;i++){
12         if(n[i]){
13             if(n[i]<10){
14                 d[j++]=n[i]+0;
15                 d[j++]=i+0;
16             }
17             else{
18                 d[j++]=n[i]/10+0;
19                 d[j++]=n[i]%10+0;
20                 d[j++]=i+0;
21             }
22         }
23     }
24     d[j]=\0;
25     return;
26 }
27 
28 int main(){
29     char num[20][85];
30     while(cin>>num[0]){
31         if(!strcmp(num[0],"-1")){//相同为0 
32             break;
33         }
34         for(int k=0;k<17;k++){
35             inventory(num[k],num[k+1]);
36         } 
37         if(!strcmp(num[0],num[1])){
38             cout<<num[0]<<" is self-inventorying"<<endl;
39             continue;
40         }
41         bool flag1=false;
42         bool flag2=false;
43         for(int i=1;i<=15;i++){
44             if(!strcmp(num[i],num[i+1])){
45                 cout<<num[0]<<" is self-inventorying after "<<i<<" steps "<<endl;
46                 flag1=true;
47                 break;
48             }
49         }
50         if(!flag1){
51             for(int i=14;i>=0;i--){
52                 if(!strcmp(num[15],num[i])){
53                     cout<<num[0]<<" enters an inventory loop of length "<<15-i<<endl;
54                     flag2=true;
55                     break;
56                 }
57             }
58         }
59         if(!flag1&&!flag2){
60             cout<<num[0]<<" can not be classified after 15 iterations"<<endl;
61         }
62     }
63 } 

70,80两个边缘数字,70刚好在15步形成循环,80步在16步形成循环

Poj 1016