题目链接:Repeating Decimals UVA - 202
题目:计算分数的循环节。
分析:数论,组合。
n除以m的余数只能是0~m-1,根据抽屉原则,当计算m+1次时至少存在一个余数相同,即为循环节;存储余数和除数,输出即可。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long
using namespace std;
int n,m;
int r[3003],u[3003],s[3003];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF) {
int t = n;
memset(r, 0, sizeof(r));
memset(u, 0, sizeof(u));
int count = 0;
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 (n && s[i] == n) printf("(");
printf("%d",r[i]);
}
if (!n) printf("(0");
if (count > 50) printf("...");
puts(")");
printf(" %d = number of digits in repeating cycle\n\n",!n ? 1 : count-u[n]);
}
return 0;
}
数论确实博大精深。。。。