Repeating Decimals, ACM/ICPC World Finals 1990, UVa202
来源:互联网 发布:apm源码解析 编辑:程序博客网 时间:2024/04/26 02:47
Repeating Decimals
Description
The decimal expansion of the fraction 1/33 is , where the 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 431 397
Sample Output
76/25 = 3.04(0) 1 = number of digits in repeating cycle5/43 = 0.(116279069767441860465) 21 = number of digits in repeating cycle1/397 = 0.(00251889168765743073047858942065491183879093198992...) 99 = number of digits in repeating cycle
#include<stdio.h>int main(){ int a, b; while(scanf("%d%d", &a, &b)!=EOF) { int c = a/b, afterpoint[3000], mod[3000], i, j, k, flag=0; printf("%d/%d = ", a, b); for(i=0; i<b; i++) { a = a%b; mod[i] = a; a *= 10; afterpoint[i] = a/b; for(j=0; j<i; j++) if(mod[j] == mod[i]) { flag = 1; break; } if(flag == 1) break; } printf("%d.", c); for(k=0; k<j; k++) printf("%d", afterpoint[k]); printf("("); if(i-j >50){ for(k=j; k< j+50; k++) printf("%d", afterpoint[k]); printf("..."); } else { for(k=j; k<i; k++) printf("%d", afterpoint[k]); } printf(")\n"); printf(" %d = number of digits in repeating cycle\n\n", i-j); } return 0;}
- Repeating Decimals, ACM/ICPC World Finals 1990, UVa202
- Repeating Decimals,ACM/ICPC World Finals 1990,UVa202
- 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
- 习题3-8 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
- Sqaures, ACM/ICPC World Finals 1990, UVa
- Uva202 - Repeating Decimals
- UVA202- Repeating Decimals
- uva202循环小数Repeating Decimals
- [UVA202]Repeating Decimals
- UVA202 UVALive5141 Repeating Decimals
- UVa202 Repeating Decimals
- UVa202-Repeating Decimals
- 3.8 Repeating Decimals UVA202
- ACM-ICPC World Finals 2017
- UVa202 ACM/ICPC 1990 循环小数
- UVa202——Repeating Decimals
- ACM-ICPC 2013 World Finals 简单题解
- Puzzle, ACM/ICPC World Finals 1993, UVa227
- 解码遇到一些问题
- Find Peak Element
- Android入门之新建HelloWorld项目报错——问题修复
- java 从键盘读入的三种形式
- 二十六、继承(三) 转换与继承、派生类到基类的转换、基类到派生类的转换
- Repeating Decimals, ACM/ICPC World Finals 1990, UVa202
- tcmalloc vs. jemalloc
- sqlite-preprocessed 编译问题总结
- 设计模式之工厂模式、建造者模式
- IO流
- Android 各层中日志打印功能的应用
- Java线程池
- 十一、判断一个数字是否是素数
- linux系统中misc子系统