题目链接:http://poj.org/problem?id=1016
最讨厌用PE来坑人的题目
这个题目很简单,但是想一次AC恐怕要注意些许细节,我栽在PE上了
题目意思直接看例子肯好理解,然后就直接按照题目意思模拟就好了
理清思路这个题目相对来说还是比较简单的!
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define maxn 100
char str[maxn];
int src[maxn],des[maxn],keep[maxn][maxn],length[maxn];
bool same(int *p,int *q,int len){
for(int i=0;i<len;i++)
if(p[i]!=q[i])
return false;
return true;
}
int main(){
int i,j,k;
bool flag;
while(scanf("%s",str)){
flag=false;
if(str[0]=='-') return 0;
for(i=0;str[i];i++){
src[i]=str[i]-'0',keep[0][i]=src[i];
}
printf("%s",str);
memset(length,0,sizeof(length));
length[0]=i;
for(k=1;k<=15;k++){
memset(des,0,sizeof(des));
for(i=0;i<length[k-1];i++)
des[src[i]]++;
for(i=0;i<10;i++){
if(des[i]==0) continue;
if(des[i]>9) src[length[k]++]=des[i]/10,src[length[k]++]=des[i]%10;
else src[length[k]++]=des[i];
src[length[k]++]=i;
}
for(i=0;i<length[k];i++)
keep[k][i]=src[i];
if(length[k]==length[k-1] && same(keep[k-1],keep[k],length[k])){
if(k==1) printf(" is self-inventorying \n");
else printf(" is self-inventorying after %d steps \n",k-1);
break;
}
for(j=0;j<k;j++){
if(length[j]==length[k] && same(keep[j],keep[k],length[k])){
printf(" enters an inventory loop of length %d \n",k-j);
flag=true;
break;
}
}
if(flag) break;
}
if(k>15) printf("can not be classified after 15 iterations\n");
}
return 0;
}