XTU 1236 fraction

Source: Internet
Author: User
Tags greatest common divisor

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.

Input

The 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].

Output

For each test case, transform X in Renshengge ' s rule.

Sample Input

3
0.9999999999999
0.3333333333333
0.2222222222222

Sample Output

1/1
1/3
2/9

Tip

You 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.