You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.2 KiB
36 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
int main() {
|
|
int r[3005], s[3005], u[3005];
|
|
int n, m;
|
|
while (scanf("%d%d", &n, &m) != EOF) {
|
|
memset(r, 0, sizeof(r));
|
|
memset(u, 0, sizeof(u));
|
|
int cnt = 0;
|
|
int t = n;
|
|
r[cnt++] = n / m;///小数点以前的数值
|
|
n = n % m;///计算余数
|
|
//!u[n]在我看来是想求循环节的长度,就是求一个相同的余数出现的次数是否超过两次
|
|
while (!u[n] && n) {
|
|
//存除数对应的循环小数位置,为了找到循环节的长度
|
|
u[n] = cnt;
|
|
s[cnt] = n;//存循环小数位置对应的除数,为了找到循环节的开始位置输出(
|
|
r[cnt++] = 10 * n / m;///得到余数
|
|
n = n * 10 % m;///得到新除数
|
|
}
|
|
printf("%d / %d = %d", t, m, r[0]);
|
|
printf(".");
|
|
for (int i = 1; i < cnt && i <= 50; i++) {
|
|
if (s[i] == n)printf("(");
|
|
printf("%d", r[i]);
|
|
}
|
|
if (cnt > 50)printf("...");
|
|
if (!n)printf("0");
|
|
printf(")\n");
|
|
printf(" %d = number of digits in repeating cycle", !n ? 1 : cnt - u[n]);
|
|
}
|
|
return 0;
|
|
}
|