zoukankan      html  css  js  c++  java
  • XTU 1236 Fraction

    Fraction

    Accepted : 168   Submit : 1061
    Time Limit : 1000 MS   Memory Limit : 65536 KB

    Fraction

    Problem 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 change all decimal fractions between 0 and 1 to fraction. In addtion, he says decimal fractions are too complicate, and set that [Math Processing Error] is much more convient than 0.33333... as an example to support his theory.

    So, RenShengGe lists a lot of numbers in textbooks and starts his great work. To his dissapoint, he soon realizes that the denominator of the fraction may be very big which kills the simplicity that support of his theory.

    But RenShengGe is famous for his persistence, so he decided to sacrifice some accuracy of fractions. Ok, In his new solution, he confines the denominator in [1,1000] and figure out the least absolute different fractions with 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 Error] .

    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 double to save x;

     
     
     
    看上去很复杂的题,其实是水题,不要被题目吓倒!
    由于分母是1-1000,所以每次将所有的分母枚举一次,选接近的数就可以了。
     
    题意:输入一个小数,输出最接近的分数,必须为最简分数。
     
    附上代码:
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 int gcd(int a,int b)
     7 {
     8     int c,t;
     9     if(a<b)
    10     {
    11         t=a,a=b,b=t;
    12     }
    13     while(b)
    14     {
    15         c=a%b;
    16         a=b;
    17         b=c;
    18     }
    19     return a;
    20 }
    21 int main()
    22 {
    23     int i,j,T;
    24     double s,minn;
    25     scanf("%d",&T);
    26     while(T--)
    27     {
    28         scanf("%lf",&s);
    29         int a=0,b=1;
    30         minn=s;
    31         for(i=1; i<=1000; i++)  //枚举1-1000的分母
    32         {
    33             j=s*i+0.5;   //求出分子
    34             double f=j*1.0/i; //计算此时分数的结果
    35             double p=fabs(f-s);  //与原来的数进行比较
    36             if(minn>p)
    37             {
    38                 minn=p;
    39                 a=j;
    40                 b=i;
    41             }
    42         }
    43         int r=gcd(a,b); //求最大公约数,化简
    44         printf("%d/%d
    ",a/r,b/r);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    初心
    [CSP-S2019]:赛后总结
    最帅的快读
    检讨书模板
    $Linux$系统$GEDIT$编译运行$C++$和各种乱搞
    [CSP-S模拟测试]:C(倍增+数学)
    [CSP-S模拟测试]:B(期望DP)
    [CSP-S模拟测试]:A(单调栈维护凸包+二分答案)
    [NOIP2018]:旅行(数据加强版)(基环树+搜索+乱搞)
    [JZOJ6347]:ZYB玩字符串(DP+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/pshw/p/5572749.html
Copyright © 2011-2022 走看看