UVa 202 Repeating Decimals
2015-02-26 17:16
295 查看
Repeating Decimals
The decimal expansion of the fraction 1/33 is tex2html_wrap_inline43 , where the tex2html_wrap_inline45 is used to indicate that the cycle 03 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number (fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have no such repeating cycles.Examples of decimal expansions of rational numbers and their repeating cycles are shown below. Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.
Write a program that reads numerators and denominators of fractions and determines their repeating cycles.
For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).
Input
Each line of the input file consists of an integer numerator, which is nonnegative, followed by an integer denominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the end of input.Output
For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle.In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins - it will begin within the first 50 places - and place “…)” after the 50th digit.
Print a blank line after every test case.
Sample Input
76 255 43
1 397
Sample Output
76/25 = 3.04(0)1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992…)
99 = number of digits in repeating cycle
分析:
循环小数的产生可以通过观察商和余数得到初步结论。 当余数开始产生重复时,循环部分已经产生,即为相同余数之间的对应小数部分(只包括后一余数)。 应用这个逻辑,可以写出代码。
代码:
// #define LOCAL #include <cstdio> #include <cstring> const int MAX_N = 3001; int a, b, zh; int de[MAX_N]; // 小数部分 int m[MAX_N]; // 余数 int flag[MAX_N]; // 余数是否重复 void disp(int a[], int n) { for (int i = 0; i <= n; i++) printf("%d%c", a[i], (i < n) ? ' ' : '\n'); } void solve() { memset(flag, 0, sizeof(flag)); memset(de, 0, sizeof(de)); memset(m, 0, sizeof(m)); int mod, t, k = 0, pre = 0; t = a; zh = a / b; while (true) { mod = t % b; if (flag[mod]) break; flag[mod] = 1; de[k] = t / b; m[k] = mod; k++; t = mod * 10; } de[k] = t / b; m[k++] = mod; for (int i = 0; i < k; i++) if (m[i] == mod) { pre = i + 1; break; } /* puts("de:"); disp(de, k); puts("mod:"); disp(m, k); puts("flag:"); disp(flag, b); */ printf("%d/%d = %d.", a, b, zh); for (int i = 1; i < pre; i++) printf("%d", de[i]); printf("("); if (k - pre > 50) { for (int i = 0; i < 50; i++) printf("%d", de[i + pre]); printf("..."); } else { for (int i = pre; i < k; i++) printf("%d", de[i]); } puts(")"); printf(" %d = number of digits in repeating cycle\n", k - pre); } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif while (~scanf("%d%d", &a, &b)) { solve(); puts(""); } return 0; }
相关文章推荐
- Uva202 - Repeating Decimals
- (Repeating Decimals) uva 202 需要一些灵感。。。
- UVA202 UVALive5141 Repeating Decimals
- (WA) 求改..UVa202 Repeating Decimals 循环小数 紫书习题3-8
- UVa 202 - Repeating Decimals
- 《算法竞赛入门经典2ndEdition 》习题3-8 循环小数(Repeating Decimals, Uva202)
- UVA_202 - Repeating Decimals
- UVA 202 Repeating Decimals
- UVa 202 - Repeating Decimals
- Uva - 202 - Repeating Decimals
- UVA - 202 Repeating Decimals
- UVa 202 Repeating Decimals(循环小数)
- UVa 202 - Repeating Decimals
- Repeating Decimals,ACM/ICPC World Finals 1990,UVa202
- UVa 202 - Repeating Decimals
- Uva - 202 - Repeating Decimals
- UVa 202 Repeating Decimals
- uva 202 Repeating Decimals
- uva202循环小数Repeating Decimals
- uva 202 Repeating Decimals 模拟