您的位置:首页 > 其它

3-8

2016-07-23 16:26 267 查看
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int q[3000]; // quotient, size of the array ?
//int r[3000]; // remainder
int s[3000]; // whether a remainder occurs, totally b possibleremainders [0..b-1] with 1 <= b <= 3000

int main()
{
int a, b;

while (scanf("%d%d", &a, &b)) {
memset(q, 0, sizeof(q));
// memset(r, 0, sizeof(r));
memset(s, 0, sizeof(s));

int num = 0;
q[num] = a / b; // q[0] is the integral part, and r[0] and s[0] are not used
printf("%d/%d = %d.", a, b, a / b);

a = a % b;
while (a != 0 && s[a] == 0) { // totally b possibleremainders [0..b-1], so the while loop will run no more than b times.
++num;
s[a] = 1;
q[num] = (10 * a) / b;
// r[num] = a;
a = (10 * a) % b;
}

if (a == 0) { // divide exactly
for (int i = 1; i <= num; i++) // [1..num]
printf("%d", q[i]);
printf("(0)\n");
printf("1 = number of digits in repeating cycle\n");
}
else {
int pos = 1;
printf("(");
for (int i = 1; i <= 50 && i<=num; i++)
{
/*
if (r[i] == a) // previous equal remainder
{
printf("(");
pos = i;
}
*/
printf("%d", q[i]);
}

if (num > 50)
printf("...");
printf(")\n");
printf("%d = number of digits in repeating cycles\n", num - pos + 1);
}
}

return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: