
Contents
Problem
題目網址
中文網址
Solution
利用長除法的方式,邊乘 10 邊除,當餘數已經重複出現時,即開始循環了。
記下餘數出現的順序,就可知道從哪開始循環了。
p.s. 循環節的長度不會大於分母。
Code
UVa 275
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
|
int () { int n, m; int order[1000];
while (scanf("%d%d", &n, &m) && m) { int count = 1; int remain[1000] = {};
while (n) { remain[n] = count; order[count++] = (n * 10) / m;
n = (n * 10) % m;
if (remain[n]) break; }
int i;
putchar('.'); for (i = 1; i < count; i++) { if (!(i % 50)) putchar('n'); printf("%d", order[i]); } putchar('n');
if (!n) puts("This expansion terminates.n"); else printf("The last %d digits repeat forever.nn", count - remain[n]); }
return 0; }
|
近期评论