XTU1236: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;

题意:

给出一个小数,求出一个最接近这个小数的分数

思路:

枚举分母,然后通过相乘得到分子

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

int t;
double s;

int main()
{
    int i,j;
    int a,b;
    double minn;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&s);
        minn = fabs(0-s);
        a = 0,b=1;
        for(i = 1; i<=1000; i++)
        {
            j = (int)(i*s+0.5);
            if(j<=i)
            {
                double f = j*1.0/i;
                double p = fabs(f-s);
                if(minn>p)
                {
                    minn = p;
                    a = j;
                    b = i;
                }
            }
        }
        int r = gcd(a,b);
        printf("%d/%d\n",a/r,b/r);
    }

    return 0;
}
时间: 2024-10-05 18:46:26

XTU1236:Fraction的相关文章

Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

[LeetCode]Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

【LeetCode】Fraction to Recurring Decimal

Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numera

【Leetcode 166】 Fraction to Recurring Decimal

Description: Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denomina

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

Here’s just a fraction of what you can do with linear algebra

Here’s just a fraction of what you can do with linear algebra The next time someone wonders what the point of linear algebra is, send them here. I write a blog on math and programming and I see linear algebra applied to computer science all the time.

Java for LeetCode 166 Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

UVA 10555 - Dead Fraction(数论+无限循环小数)

UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,并且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示为n/m形式,并且a为整数部分有c位, b为小数部分有d位 那么aaaaa.bbb...和aaaaabbb....分别可以表示为10c?(n/m)和10c+d?(n/m) 两式相减得:aaaaabbb?aaaaa=(10c+d?10c)(n/m) 那么设n1 = aaaaabbb ,n2 = aaa

166. Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu