题目传送门:Repeating Decimals
题意:
输入a,b,求a/b的小数表示,并求出循环节长度。
分析:
不断用a整除b,得出的数为小数点后的数,记录下来,然后再用a = a%b*10再去整除b,直到出现相同的a位置。
AC代码:
(注意格式空行,在下pe了N遍。。。。。)
//#include<bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include<algorithm>
//#include <map>
//#include <set>
//#include <vector>
//#include <queue>
//#include <stack>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
#define MAXN 10005
#define INF 0x3f3f3f3f//将近ll类型最大数的一半,而且乘2不会爆ll
const ll mod = 1000000007;
const ll inv = (mod + 1) / 2;
int up[MAXN], down[MAXN], ans[MAXN];
int iff(int *arr, int x, int y) {
for (int i = 2; i <= x; ++i)
if (arr[i] == y) return i;
return 0;
}
int main()
{
int a, b;
while (cin >> a >> b) {
memset(up, 0, sizeof(up));
memset(down, 0, sizeof(down));
memset(ans, 0, sizeof(ans));
int i = 1, j = 1, m=1;
up[i] = a, down[j] = a / b, ans[m] = a%b;
while (!iff(up, i, ans[i]*10)) {
i++; j++; m++;
up[i] = ans[m - 1] * 10;
down[j] = up[i] / b;
ans[m] = up[i] % b;
}
int yy = iff(up, i, ans[i] * 10);
printf("%d/%d = ", a, b);
for (int e = 1; e <= i; ++e) {
if (e == yy) cout << '(';
cout << down[e];
if (e == 1) cout << '.';
if (e > 50) { cout << "..."; break;}
}
cout << ')' << '\n';
cout << " " << i - yy + 1 << " = number of digits in repeating cycle" << '\n' << '\n';
}
return 0;
}