ITeye博客

poj 1016 numbers that count

antsmall · 2011-01-22 00:11 · algorithm · 编程语言 · Markdown

/*

* poj 1016 numbers that count

* antsmall 2010-11-7

*/

#include<iostream>
using namespace std;

const int MAX_LEN = 81;
int cnt[10];
char his[17][MAX_LEN];

void trans(char* src, char* dest) {
int len = strlen(src);
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < len; i++) {
   cnt[src[i]-'0']++;
}
int index = 0;
for(int i = 0; i < 10; i++) {
   if(cnt[i] > 0) {
    if(cnt[i] > 9) {
     dest[index++] = ('0'+cnt[i]/10);
     dest[index++] = ('0'+cnt[i]%10);
    } else {
     dest[index++] = ('0'+cnt[i]);
    }
    dest[index++] = ('0'+i); 
   }
}
dest[index]='\0';
}

int main() {
char* src = new char[MAX_LEN];
char* dest = new char[MAX_LEN];
char* tmp = new char[MAX_LEN];
char oldData[16][MAX_LEN];

int i;
bool fin;
int oldindex = 0;
while(cin>>src && strcmp(src,"-1")!=0 ) {
   fin = false;
   strcpy(dest, src);
   for(i = 0; i < 15; i++) {
    trans(dest, tmp); 
    strcpy(his[i], dest);
    if(i == 0 && strcmp(src, tmp) == 0) {
     cout<<src<<" is self-inventorying"<<endl;
     fin = true;
     break;
    } 
    if(i > 0 && strcmp(dest, tmp) == 0) {
     cout<<src<<" is self-inventorying after "<<i<<" steps"<<endl;
     fin = true;
     break;
    }
    for(int j = 0; !fin && j <= i; j++) {
     if(strcmp(his[j], tmp)==0) {
      cout<<src<<" enters an inventory loop of length "<<i-j+1<<endl;
      fin = true;
      break;
     }
    }
    strcpy(dest, tmp);
   }
   if(!fin) {
    cout<<src<<" can not be classified after 15 iterations"<<endl;
   }
}
//system("pause");
return 0;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。