UVa 202 - Repeating Decimals

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

UVa 275 差不多,但格式很麻煩。

記得有限小數時後面還是要補 (0) 。

Code

UVa 202
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<cstdio>

int main()
{
int x, y;
int remain[3000], order[3001] = {};
while (scanf("%d%d", &x, &y) != EOF)
{
int n = x, count = 1, i;
bool part = false;

for (i = 0; i < y; i++)
remain[i] = 0;

n %= y;
while (n&&!remain[n])
{
remain[n] = count;
order[count++] = n * 10 / y;

n = (n * 10) % y;
}

printf("%d/%d = %d", x, y, x / y);
putchar('.');

if (n)//無限小數
{
for (i = 1; i < remain[n]; i++)
printf("%d", order[i]);
putchar('(');
int temp = 50;
for (; i < count&&temp; i++, temp--)
printf("%d", order[i]);
printf("%s", i == count ? ")" : "...)");
}
else//有限小數
{
for (i = 1; i < count; i++)
printf("%d", order[i]);
printf("(0)");
}

printf("\n %d = number of digits in repeating cycle\n\n", n ? (count - remain[n]) : 1);
}

return 0;
}