题目大意:
来源:https://blog.csdn.net/baoqiaoben/article/details/80085965
#include <iostream> #include <sstream> #include <iomanip> #include <string> #include <numeric> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <utility> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; typedef long long ll; const int MAXN = 100; const int MOD7 = 1000000007; const int MOD9 = 1000000009; const int INF = 2000000000;//0x7fffffff const double EPS = 1e-9; const double PI = 3.14159265358979; const int dir_4r[] = { -1, 1, 0, 0 }; const int dir_4c[] = { 0, 0, -1, 1 }; const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; int cnt[MAXN]; char digits[MAXN]; char buf1[MAXN]; char buf2[MAXN]; bool loop;//判断是否出现环 int loopsize; map<string, int> mp; void fun(char *str, int &index, int cnt) { if (cnt == 0) return; int d = cnt % 10; cnt /= 10; fun(str, index, cnt); str[index++] = d + ‘0‘; } void inventory(char *in, char *out) { memset(cnt, 0, sizeof(cnt)); for (int i = 0; in[i]; ++i) cnt[in[i] - ‘0‘]++; int index = 0; for (int i = 0; i < 10; ++i) { if (cnt[i]) { fun(out, index, cnt[i]); out[index++] = i + ‘0‘; } } out[index++] = ‘\0‘; } bool isSelfInventory(char *buf1, char *buf2) { inventory(buf1, buf2); if (strcmp(buf1, buf2) == 0) return true; else return false; } int solve() { int index = 0; map<string, int>::iterator it; mp.clear(); loop = false; loopsize = INF; strcpy(buf1, digits); while (index <= 15) { if (isSelfInventory(buf1, buf2)) break; it = mp.find(string(buf1)); if (it != mp.end()) { loop = true; loopsize = min(loopsize, index - it->second); } mp[string(buf1)] = index++; strcpy(buf1, buf2); } return index; } int main() { while (scanf("%s", digits)) { if (digits[0] == ‘-‘) break; int ans = solve(); if (ans == 0) printf("%s is self-inventorying\n", digits); else if (ans <= 15) printf("%s is self-inventorying after %d steps\n", digits, ans); else if (loop) printf("%s enters an inventory loop of length %d\n", digits, loopsize); else printf("%s can not be classified after 15 iterations\n", digits); } //system("pause"); return 0; }
原文:https://www.cnblogs.com/sweet-ginger-candy/p/11518181.html