POJ1930Dead Fraction
来源:互联网 发布:vb.net与数据库连接 编辑:程序博客网 时间:2024/06/06 01:37
Dead Fraction
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2097 Accepted: 669
Description
Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by "...". For instance, instead of "1/3" he might have written down "0.3333...". Unfortunately, his results require exact fractions! He doesn't have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
Input
There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case.
Output
For each case, output the original fraction.
Sample Input
0.2...0.20...0.474612399...0
Sample Output
2/91/51186531/2500000看别人的题解做的
package 数学问题的解题窍门.辗转相除法;import java.util.Scanner;public class Dead_Fraction {/** * @param args */static int len, i, j, a, b, all, num, c, k;static String g;public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);while((g=cin.next())!=null){len = g.length(); if(len==1 && g.charAt(0)=='0'){break;}a = b = 1000000000;all = num = 0;c = k = 1;for(i=2; i<len; i++){if(g.charAt(i)== '.')break;all*=10;all+=g.charAt(i)-'0';c*=10;}//System.out.println(all);num = all;while(c>1){num /= 10;k *= 10;c /= 10;j = GCD(all-num, c*(k-1));//System.out.println((all-num) + ", " + (c*(k-1)) +", "+j);if(c*(k-1)/j<b){a = (all-num)/j;b = c*(k-1)/j;}}System.out.println(a+"/"+b);}}private static int GCD(int l, int m) {if(l%m==0)return m;return GCD(m,l%m);}}
0 0
- POJ1930Dead Fraction
- Fraction
- Fraction
- XTU1236:Fraction
- proper fraction
- XTU1236 Fraction
- HDU5912-Fraction
- HDU5912 Fraction
- HDU5912-Fraction
- HDU5912 Fraction
- hdu5912 Fraction
- A. Fraction
- POJ 1930 Dead Fraction
- Continued Fraction of Squareroot
- POJ_1980_Unit Fraction Partition
- SZU:G32 Mass fraction
- POJ-1930-Dead Fraction
- An Irreducible Fraction
- <start>
- [Android]类似淘宝的数字自增自减文本
- leetcode Search in Rotated Sorted Array
- JavaBean的方法的命名必须遵循的规则
- ZOJ 3870 Team Formation && ZOJ 3872Beauty of Array
- POJ1930Dead Fraction
- Uva 1636
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- 程序入口函数和glibc及C++全局构造和析构
- RAID
- 3G行业面试出的Java编程题
- linux uvc usb camera driver
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Android提供第三方jar包时资源打包