首页 > 其他 > 详细

XTU 1236 Fraction(二分)

时间:2015-06-14 16:44:01      阅读:89      评论:0      收藏:0      [点我收藏+]

Fraction

Accepted : 51   Submit : 435
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;

题意:把一个小数化成分数,分母不会超过1000。

题解:首先要注意的是如果x<0.001,答案就是0和1/1000最接近x的那个;

           先预处理小数,排序,再二分找到最接近x的那个数。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;
struct node {
    double x;
    int d,f;
} s[1000010];
int len;

int cmp(node a,node b) {
    if(a.x==b.x) {
        return a.f<b.f;
    }
    return a.x<b.x;
}

void  init() {
    len=0;
    s[len].x=1.0;
    s[len].d=1;
    s[len++].f=1;
    s[len].x=0.0;
    s[len].d=1;
    s[len++].f=0;
    for(int i=2; i<=1000; i++) {
        for(int j=1; j<i; j++) {
            double k=j*1.0/(i*1.0);
            s[len].x=k;
            s[len].d=i;
            s[len++].f=j;
        }
    }
    sort(s,s+len,cmp);
}

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

int main() {
    init();
    int t;
    scanf("%d",&t);
    double x;
    while(t--) {
        scanf("%lf",&x);
        int l=0,r=len-1;
        int L=0,R=len-1;
        int mid=(l+r)>>1;
        int id=-1;
        while(l<r) {
            mid=(l+r)>>1;
            if(s[mid].x==x) {
                id=mid;
                break;
            }
            if(s[mid].x>x) {
                R=r;
                r=mid-1;
            } else {
                L=l;
                l=mid+1;
            }
        }
        int F,D;
        if(id==-1) {//找最接近
            double Min=1;
            for(int i=L; i<=R; i++) {
                double p=fabs(x-s[i].x);
                if(Min>p) {
                    Min=p;
                    id=i;
                }
            }
        }
        F=s[id].f;
        D=s[id].d;
        int k=gcd(D,F);
        printf("%d/%d\n",F/k,D/k);
    }
    return 0;
}


XTU 1236 Fraction(二分)

原文:http://blog.csdn.net/acm_baihuzi/article/details/46491791

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!