题目连接:http://poj.org/problem?id=1016
题目没什么好说的,就是模拟,可以用string来简化代码。
代码:
Source Code
Problem: 1016 | User: bingshen | |
Memory: 236K | Time: 141MS | |
Language: C++ | Result: Accepted |
- Source Code
#include<iostream> #include<string> using namespace std; string str[20]; void dfs(string input,int index) { int i,num[10]={0}; string temp; if(index==16) return; for(i=0;i<input.length();i++) num[input[i]-'0']++; for(i=0;i<10;i++) { if(num[i]) { if(num[i]<10) temp.insert(temp.end(),num[i]+'0'); else { temp.insert(temp.end(),num[i]/10+'0'); temp.insert(temp.end(),num[i]%10+'0'); } temp.insert(temp.end(),i+'0'); } } str[index]=temp; dfs(temp,index+1); } int findloop(int index) { int i; for(i=index+1;i<=15;i++) if(str[i]==str[index]) return (i-index); return 0; } int main() { int loop,i; bool flag,after; string input; while(cin>>input) { if(input=="-1") break; str[0]=input; after=true; dfs(input,1); if(str[0]==str[1]) { cout<<str[0]<<" is self-inventorying"<<endl; continue; } for(i=1;i<=15;i++) { if(str[i-1]==str[i]) { after=false; cout<<str[0]<<" is self-inventorying after "<<i-1<<" steps"<<endl; break; } } if(after) { flag=true; for(i=0;i<=15;i++) { loop=findloop(i); if(loop) { flag=false; cout<<str[0]<<" enters an inventory loop of length "<<loop<< endl; break; } } if(flag) cout<<str[0]<<" can not be classified after 15 iterations"<<endl; } } return 0; }