Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2097 | Accepted: 669 |
Description
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
Output
Sample Input
0.2... 0.20... 0.474612399... 0
Sample Output
2/9 1/5 1186531/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 stub
Scanner 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);
}
}