Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
测试数据都对,一直WA。求高人指点,谢谢#include <iostream> #include <string> #include <string.h> #include <cstring> #include <stdio.h> using namespace std; char initdigits[81]; char iterations[16][81]; int digitcount[10]; int steps; bool flag; void transform(int n) { for (int i = 0; i < 10; i++) digitcount[i] = 0; for (int i = 0; iterations[n][i] != 0; i++) { switch (iterations[n][i]) { case '0': digitcount[0]++; break; case '1': digitcount[1]++; break; case '2': digitcount[2]++; break; case '3': digitcount[3]++; break; case '4': digitcount[4]++; break; case '5': digitcount[5]++; break; case '6': digitcount[6]++; break; case '7': digitcount[7]++; break; case '8': digitcount[8]++; break; case '9': digitcount[9]++; break; } } int temp = 0; for (int i = 0; i < 10; i++) { if (digitcount[i]) { if (digitcount[i] >= 10) { iterations[n+1][temp++] = digitcount[i] / 10 + '0'; iterations[n+1][temp++] = digitcount[i] % 10 + '0'; } else { iterations[n+1][temp++] = digitcount[i] % 10 + '0'; } iterations[n+1][temp++] = i + '0'; } } } int finddigits(int n) { for (int i = n - 1; i >= 0; i--) { if (strcmp(iterations[i], iterations[n]) == 0) return i; } return -1; } int main() { memset(initdigits, 0, sizeof(initdigits)); memset(iterations, 0, sizeof(iterations)); while (cin >> initdigits) { if (strcmp(initdigits, "-1") == 0) break; flag = false; steps = 0; strcpy_s(iterations[0], initdigits); while (steps <= 15) { transform(steps); steps++; if (strcmp(iterations[steps - 1], iterations[steps]) == 0) { if(steps==1) cout << initdigits << " is self-inventorying" << endl; else cout << initdigits << " is self-inventorying after " << (steps - 1) << " steps" << endl; flag = true; break; } } steps = 1; while (steps <= 15 && flag == false) { if (finddigits(steps) != -1) { cout << initdigits << " enters an inventory loop of length " << (steps - finddigits(steps)) << endl; flag = true; break; } steps++; } if (!flag) { cout << initdigits << " can not be classified after 15 iterations" << endl; } memset(initdigits, 0, sizeof(initdigits)); memset(iterations, 0, sizeof(iterations)); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator