zoukankan      html  css  js  c++  java
  • XTU1236 Fraction

    Fraction

    Accepted : 124
    Submit : 806
    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  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 .

    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;


    Source

    XTU OnlineJudge

    题意:把小数化成最接近他的分数,要求最简形式。

    分析:枚举分母。每次得到的分子与初始值相比較误差最小的就是答案。


    <span style="font-size:18px;">#include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const double eps = 1e-6;
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int MOD = 1000000007;
    #define ll long long
    #define CL(a,b) memset(a,b,sizeof(a))
    #define lson (i<<1)
    #define rson ((i<<1)|1)
    #define MAXN 100010
    
    int main()
    {
        int T;
        double s,minx;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%lf",&s);
            minx = s;
            int a=0,b=1;
            for(int i=1; i<=1000; i++)
            {
                int j = i*s+0.5;
                double f = j*1.0/i;
                double p = fabs(f-s);
                if(minx > p)
                {
                    minx = p;
                    a = j;
                    b = i;
                }
            }
            int r = __gcd(a, b);
            printf("%d/%d
    ",a/r,b/r);
        }
        return 0;
    }
    </span>


  • 相关阅读:
    关于oracle当中数据类型转换的问题
    CASE WHEN的两种格式
    C#设置默认打印机
    运用Merge Into实现增加或更新数据
    增加或修改的存储过程
    深拷贝与浅拷贝
    sql server两种分页方法
    获取sql执行时间
    inserted触发器,一张表插入数据时,同时向另外一张表插入数据
    List<string[]> 如何去重
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7349644.html
Copyright © 2011-2022 走看看