2015 湘潭邀请赛 Fraction

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;


湘潭赛据说最简单的一道题。。题意就是给出一个小数,求出一个最接近这个小数的分数,做题的时候被前面一大堆英文什么的绕进去了,完全没有想到用正常的方法做!浪费了好多时间!

这题用枚举+二分就可以了。把1000*1000所有的可能性打表,从小到大按照实际值大小排序,然后二分查找。。。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 501000;

struct node
{
    int x, y;
};
node stu[maxn];
bool cmp (node a, node b)
{
    return a.x*1.0/a.y < b.x*1.0/b.y ? true : false;
}

int gcd (int a, int b)
{
    return a%b==0?b:gcd(b, a%b);
}

int Bin_Sreach(double n, int low, int high)
{
    while (low <= high)
    {
        int mid = (low + high) / 2;
        double num = stu[mid].x * 1.0 / stu[mid].y;
        if (num < n)
            low = mid;
        if (num > n)
            high = mid;
        if (high - low == 1)
            break;
    }
    double num1 = stu[low].x*1.0 / stu[low].y;
    double num2 = stu[high].x*1.0 / stu[high].y;
    if (fabs(n - num1) > fabs(n - num2))
        return high;
    return low;
}
int main ()
{
    int t, k = 1;
    
    stu[0].y = 1;
    for (int i=1; i<=1000; i++)
        for (int j=i; j<=1000; j++)
        {
            if (gcd(j, i) == 1)
            {
                stu[k].x = i;
                stu[k++].y = j;
            }
        }
        sort (stu, stu+k, cmp);
        
        scanf ("%d", &t);
        while (t --)
        {
            double n;
            scanf ("%lf", &n);
            int p = Bin_Sreach(n, 0, k-1);
            printf ("%d/%d\n", stu[p].x, stu[p].y);
        }
        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值