Repeating Decimals
来源:互联网 发布:网络教育毕业总结 编辑:程序博客网 时间:2024/06/16 04:58
The decimal expansion of the fraction 1/33 is 0.03, where the 03 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.
Sample Input
76 25
5 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
题意:计算一个分数的循环小数的位数
方法:作逐步除法,判断是否有重复的商,若有,那么循环的小数必然在这两个商之间
#include <iostream>#include <stdio.h>#include <memory.h>const int MAX_CYCLE_LEN = 3005;const int MAX_POS_LEN = 3005;const int LIMIT = 50;int reminder_exist[MAX_POS_LEN];int reminder_pos[MAX_POS_LEN];using namespace std;int main(){ int numerator, demoninator; int quot, reminder; while(scanf("%d %d", &numerator, &demoninator) != EOF && demoninator) { int orig_numerator = numerator; int cycle_len, cycle_start; int digit[MAX_CYCLE_LEN]; int Integer; memset(reminder_exist, 0, sizeof(reminder_exist)); memset(reminder_pos, 0, sizeof(reminder_pos)); quot = numerator / demoninator; reminder = numerator % demoninator; Integer = quot; for(int i=0;;i++) { if(reminder_exist[reminder]) { cycle_start = reminder_pos[reminder]; cycle_len = i - cycle_start; break; } else { reminder_exist[reminder] = 1; reminder_pos[reminder] = i; } numerator = reminder * 10; quot = numerator / demoninator; reminder = numerator % demoninator; digit[i] = quot; } printf("%d/%d = %d.", orig_numerator, demoninator, Integer); for(int i=0; i<cycle_start; i++) printf("%d", digit[i]); printf("("); if(cycle_len <= LIMIT) { for(int i=cycle_start; i<cycle_len + cycle_start; i++) printf("%d", digit[i]); printf(")\n"); } else { for(int i=cycle_start; i<LIMIT + cycle_start; i++) printf("%d", digit[i]); printf("...)\n"); } printf(" %d = number of digits in repeating cycle\n", cycle_len); printf("\n"); } return 0;}
- Repeating Decimals
- Repeating Decimals
- Repeating Decimals
- Uva202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVA_202 - Repeating Decimals
- UVaOJ 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- 202 - Repeating Decimals
- UVA202- Repeating Decimals
- UVa 202 Repeating Decimals
- uva202循环小数Repeating Decimals
- 232 - Repeating Decimals
- Uva 202 Repeating Decimals
- 循环小数(Repeating Decimals)
- UVA - 202 Repeating Decimals
- 202 - Repeating Decimals
- hdu 1864 最大报销额
- 前端通过AJAX提交数据,后台PHP处理数据实现异步操作基本流程
- [BZOJ3790]神奇项链-manachar
- PAT 1094. The Largest Generation 用数组确定父亲,向上遍历确定相互联系
- 自媒体/新媒体写作技巧
- Repeating Decimals
- Android省市区三级联动和地区定位
- TCP/IP三次握手和四次挥手
- POJ 2241 The Tower of Babylon 笔记
- Java包命名规则
- [Freemarker] freemarker内建函数
- 栈的各种操作
- QT总结之TCP连接
- 关于Flex的ScrollBar