即为循环节;存储余数和除数,输出即可。
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; int chu[3000], yu[3000]; int main() { int a, b; while (scanf("%d%d", &a, &b) != EOF) { int t1, t2, t3, length = 0, cycle = -1, begin = -1; memset(chu, 0, sizeof(int) * 3000); memset(yu, 0, sizeof(int) * 3000); t2 = a / b; t3 = a%b; printf("%d/%d = %d", a, b, t2); if (t3 == 0) { printf(".(0)\n 1 = number of digits in repeating cycle\n\n"); } else { printf("."); while (1) { yu[length] = t3; t1 = t3 * 10; chu[length++] = t1 / b; t3 = t1%b; if (t3 == 0) break; else { bool flag = 0; for (int i = 0; i < length; i++) if (t3 == yu[i]) { flag = 1; begin = i; cycle = length - begin; break; } if (flag) break; } } if (begin == -1) { if (length <= 50) { for (int i = 0; i < length; i++) printf("%d", chu[i]); } else { for (int i = 0; i < 50; i++) printf("%d", chu[i]); printf("..."); } printf("(0)\n 1 = number of digits in repeating cycle\n\n"); } else { for (int i = 0; i < begin; i++) printf("%d", chu[i]); printf("("); if (begin + cycle >= 50) { for (int i = begin; i < 50; i++) printf("%d", chu[i]); printf("...)\n %d = number of digits in repeating cycle\n\n", cycle); } else { for (int i = begin; i < length; i++) printf("%d", chu[i]); printf(")\n %d = number of digits in repeating cycle\n\n", cycle); } } } } return 0; }