题目:计算分数的循环节。
https://vjudge.net/contest/227853#problem/C
分析:数论,组合。
n除以m的余数只能是0~m-1,根据抽屉原则,当计算m+1次时至少存在一个余数相同,
即为循环节;存储余数和除数,输出即可。
#include <bits/stdc++.h>
using namespace std;
int r[3003],u[3003],s[3003];int main() {int n, m, t;while (scanf("%d%d", &n, &m)!=EOF) {memset(r, 0, sizeof(r));memset(u, 0, sizeof(u));int count = 0;int t = n;r[count++] = n/m;n = n%m;while (!u[n] && n) {u[n] = count;//存除数对应的循环小数位置,为了找到循环节长度s[count] = n;//存循环小数位置对应的除数,为了找到循环节的开始位置输出"("r[count++] = 10 * n / m;//得到余数n = 10 * n%m;//得到新除数}printf("%d/%d = %d.",t,m,r[0]);for (int i = 1; i < count&&i<=50; i++) {if (s[i] == n)printf("(");printf("%d",r[i]);}if (count>50)printf("...");if (!n)printf("(0");printf(")\n");printf(" %d = number of digits in repeating cycle\n\n",!n?1:count-u[n]);}
}