模拟
View Code
#include#include using namespace std;string origin;string tostring(int a){ string ans, l; ans = ""; while (a) { l = ""; l += a % 10 + '0'; ans.insert(0, l); a /= 10; } return ans;}string make(string a){ int i, times[10]; string ans; memset(times, 0, sizeof(times)); for (i = 0; i < a.length(); i++) times[a[i] - '0']++; ans = ""; for (i = 0; i < 10; i++) { if (!times[i]) continue; ans += tostring(times[i]); ans += i + '0'; } return ans;}void work(){ string trans[20]; int i, j; trans[0] = origin; for (i = 1; i <= 15; i++) { trans[i] = make(trans[i - 1]); if (trans[i] == trans[i - 1]) { if (i == 1) cout << origin << " is self-inventorying\n"; else cout << origin << " is self-inventorying after " << i - 1 <<" steps\n"; return; } for (j = 0; j < i - 1; j++) if (trans[i] == trans[j]) { cout << origin << " enters an inventory loop of length " << i - j << endl; return; } } cout << origin << " can not be classified after 15 iterations\n";}int main(){ //freopen("t.txt", "r", stdin); while (cin >> origin && origin != "-1") { work(); } return 0;}