题记:参考了大佬的代码
https://blog.csdn.net/mobius_strip/article/details/39870555
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int r[3003], u[3003], s[3003];
int main()
{
int n, m, t;
while (cin >> n >> m)
{
t = n;
memset(r, 0, sizeof(r));
memset(u, 0, sizeof(u));
int count = 0;
r[count++] = n / m;//r[0]存放小数点之前的数
n = n % m;
while (!u[n] && n)//当这个余数没有出现过且n不为0(即余数不为0)时
{
u[n] = count;//记录下这个余数的出现
s[count] = n;//记录余数,用于判断循环节的开始
r[count++] = 10 * n / m;//小数点之后的数字
n = 10 * n % m;//计算之后的余数
}
printf("%d/%d = %d", t, m, r[0]);
printf(".");
for (int i = 1; i < count && i <= 50; i++)
{
if (n && s[i] == n)printf("(");//判断循环节的开始地方
printf("%d", r[i]);
}
if (!n)printf("(0");//如果n为0则循环数为0
if (count > 50)printf("...");//题目要求输出50个数之后输出...
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n", !n ? 1 : count - u[n]);
//如果循环数为0则输出1
}
return 0;
}