UVA - 202
感谢Udebug,自己写的bug是真的多。
易错点:
1.可以整除的数小数位是0,但是要置1
2.在寻找循环节时要从小数部分开始记录,整数部分不能记录
3.根据抽屉原理循环节长度
4.注意换行
Code
//#define HelloWorld
#include
#define MAX 100005
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
using namespace std;
typedef long long LL;
int point[MAX], pos[MAX];
bool vis[MAX];
int main() {
#ifdef HelloWorld
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int a, b, res, rem;
while (cin >> a >> b) {
int temp = a;
int flag = 0, sum = 0;
memset(vis, 0, sizeof(vis));
memset(pos, -1, sizeof(pos));
while (true) {
res = a / b;
rem = a % b;
if (flag) {
point[sum] = res;
vis[a] = true;
pos[a] = sum;
sum++;
}
a = rem * 10;
if (vis[a]) break;
flag = 1;
}
cout << temp << "/" << b << " = " << temp / b << ".";
int cnt;
if (sum > 50) flag = 0, cnt = 50;
else flag = 1, cnt = sum;
if (!cnt)
cout << "(0";
else {
for (int i = 0; i < cnt; i++) {
if (i == pos[a]) cout << "(";
cout << point[i];
}
}
if (!flag) cout << "...";
cout << ")" << endl;
int ans = sum - pos[a];
if (ans == 0) ans = 1;
cout << " " << ans << " = number of digits in repeating cycle" << endl;
cout << endl;
}
return 0;
}