poj 2325 Persistent Numbers (贪心+高精度)
把输入数字每次从9-2除,能整除则记录该数字,最后从小到大输出。
应该算是水题,不过窝第一次写高精度除法,虽然1A,不过中间改了好多次。
/****************************************** Problem: 2325 User: Memory: 684K Time: 110MS Language: G++ Result: Accepted ******************************************/ #include <iostream> #include <cstdio> #include <cstring> using namespace std; char numStr[1005]; int num[1005]; int num1[1005]; int ans[1005]; int len; int cnt; int in;//因为数字位数会越除越小,用in来记录少了多少位 bool div(int n) { int i; int temp = (num[in] < n); for (i = in; i < len; ++i) num1[i] = num[i]; for (i = in; i < len; ++i) { num1[i + 1] += (num1[i] % n) * 10; num1[i] /= n; } if (num1[i]) { num1[i] = 0; return false; } else { in += temp; for (i = in; i < len; ++i) num[i] = num1[i]; return true; } } int main() { int i; while (scanf("%s", numStr) != EOF) { len = strlen(numStr); if (len == 2 && numStr[0] == '-' && numStr[1] == '1') break; if (len == 1) { printf("%c%s\n", '1', numStr); continue; } in = cnt = 0; memset(num1, 0, sizeof num1); memset(num, 0, sizeof num); for (i = 0; i < len; ++i) { num[i] = numStr[i] - '0'; } while (!(len - in == 1 && num[in] == 1)) { //printf("in:%d\n", in); for (i = 9; i >= 2; --i) { if (div(i)) { ans[cnt++] = i; break; } } //printf("i=%d\n", i); if (i == 1) break; } if (i == 1) printf("There is no such number."); else for (i = cnt - 1; i >= 0; --i) printf("%d", ans[i]); printf("\n"); } return 0; }