XTU 1236 Fraction(二分)

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;
}
时间: 2024-10-07 15:58:10

XTU 1236 Fraction(二分)的相关文章

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 th

xtu 1236 Fraction(小数化分数)

地址:点击打开链接 代码: #include<cstdio> using namespace std; double abss(double x) { return x>0.0?x:-x; } int main() { int t; scanf("%d",&t); while(t--) { double x; scanf("%lf",&x); if(x==0.0) { printf("0/1\n"); conti

XTU 1185 暴力打表+二分

题目连接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1185 Bob's Problem Accepted : 105   Submit : 550 Time Limit : 1000 MS   Memory Limit : 65536 KB 题目描述 Bob今天碰到一个问题,他想知道x3+y3 = c 是否存在正整数解? 输入 第一行是一个整数K(K≤20000),表示样例的个数. 以后每行一个整数c(2≤c≤10

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y

xtu数据结构 H. City Horizon

H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silh

HihoCoder 1236 Scores - bitset - 分块

Kyle is a student of Programming Monkey Elementary School. Just as others, he is deeply concerned with his grades. Last month, the school held an examination including five subjects, without any doubt, Kyle got a perfect score in every single subject

UVA1616-Caravan Robbers(二分)

Problem UVA1616-Caravan Robbers Accept: 96  Submit: 946Time Limit: 3000 mSec Problem Description Long long ago in a far far away land there were two great cities and The Great Caravan Road between them. Many robber gangs "worked" on that road. B

2.1 二分分类

本周学习神经网络编程的基础知识 构建神经网络,有些技巧是非常重要 神经网络的计算过程中,通常有一个正向的过程(正向传播步骤),接着会有一个反向步骤(反向传播步骤), 为什么神经网络的计算可以分为前向传播和反向传播两个分开的过程?本周课程通过使用logistic回归来阐述,以便于能够更好的理解, logistic回归是一个用于二分分类的算法 比如有一个二分分类问题的例子, 假如有一张图像作为输入是这样的,你想输出识别此图的标签,如果是猫,输出1,如果不是,则输出0 使用y来表示输出的结果标签, 来

HDU3715(二分+2-SAT)

Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3184    Accepted Submission(s): 1035 Problem Description Here is a procedure's pseudocode: go(int dep, int n, int m)beginoutput the valu