Fraction |
accepted:168 |
|
submit:1061 |
Time limit:1000 MS |
|
Memory limit:65536 KB |
Fractionproblem Description: Everyone has silly periods, especially for Renshengge. It's A sunny day, no one knows "what happened to Renshengge," Renshengge says that he wants to the change all decimal fractions Between 0 and 1 to fraction. In addtion, he says decimal fractions was too complicate, and set that [Math processing Error] was much more co Nvient than 0.33333 ... As an example-to-support his theory. So, Renshengge lists a lot of numbers in textbooks and starts he great work. To him dissapoint, he soon realizes that the denominator of the fraction is very big which kills the simplicity that s Upport of his theory. But Renshengge was famous for his persistence, so he decided to sacrifice some accuracy of fractions. Ok, in him new solution, he confines the denominator in [1,1000] and figure out the least absolute different fractions wit H The decimal fraction under his restriction. If several fractions satifies the restriction, he chooses the smallest one with simplest formation. InputThe first line contains a number T (no more than 10000) which represents the number of test cases. And there followed T lines, each line contains a finite decimal fraction x that satisfies [Math processing ERR or]. OutputFor each test case, transform X in Renshengge ' s rule. Sample Input3 0.9999999999999 0.3333333333333 0.2222222222222 Sample Output1/1 1/3 2/9 TipYou can use a double to save X; |
Looks very complicated problem, actually is water question, do not be intimidated by the topic! Since the denominator is 1-1000, each time you enumerate all the denominators, you can select the nearest number. Test instructions: Enter a decimal number to output the nearest score, which must be the simplest fraction. Attached code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 intgcdintAintb)7 {8 intc,t;9 if(a<b)Ten { Onet=a,a=b,b=T; A } - while(b) - { thec=a%b; -A=b; -b=C; - } + returnA; - } + intMain () A { at inti,j,t; - DoubleS,minn; -scanf"%d",&T); - while(t--) - { -scanf"%LF",&s); in intA=0, b=1; -minn=s; to for(i=1; i<= +; i++)//the denominator of enumeration 1-1000 + { -j=s*i+0.5;//Find the Molecule the Doublef=j*1.0/I;//calculate the result of this fraction * DoubleP=fabs (f-s);//compare to the original number $ if(minn>p)Panax Notoginseng { -minn=p; theA=J; +b=i; A } the } + intR=GCD (A, b);//seeking greatest common divisor, simplifying -printf"%d/%d\n", a/r,b/R); $ } $ return 0; -}
XTU 1236 fraction