GYM 101653 X.Wrench Peter(水~)

Description

给出一个十进制小数,其小数部分必然为 A / B 的形式上取整或下取整,其中 B 为介于 2 ~ 128 之间的 2 的幂次, 1 A < B ,输出满足条件的 A , B ,要求 B 最小

Input

第一行一整数 T 表示用例组数,每组用例输入一个长度不超过 10 的十进制小数 ( 1 T 100 )

Output

如果是整数则直接输出该整数,如果是小于 1 的小数则输出其分数表示,否则输出其整数部分和其小数部分的分数表示

Sample Input

10
0.81
.8125
0.37
2
2.4
2.99
2.40
1.27
4.
9.242187

Sample Output

13/16”
13/16”
3/8”
2”
2 3/8”
2 63/64”
2 13/32”
1 17/64”
4”
9 31/128”

Solution

简单题,枚举 A , B 暴力判断即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
char s[maxn];
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int n=strlen(s),pos=n;
        for(int i=0;i<n;i++)
            if(s[i]=='.')
            {
                pos=i;
                break;
            } 
        ll x=0;
        for(int i=0;i<pos;i++)x=10ll*x+s[i]-'0';
        int tar=0,temp=1;
        for(int i=pos+1;i<n;i++)tar=10*tar+s[i]-'0',temp=10*temp;
        int p,q=-1;
        if(tar)
        {
            for(int i=2;i<=128;i*=2)
            {
                for(int j=1;j<i;j++)
                {
                    int Floor=(int)(1.0*j*temp/i);
                    int Ceil=Floor;
                    if((j*temp)%i)Ceil++;
                    if(tar==Floor||tar==Ceil)
                    {
                        p=j,q=i;
                        break;
                    }
                }
                if(q!=-1)break;
            }
        }
        if(x)
        {
            printf("%lld",x);
            if(tar)printf(" ");
        }
        if(tar)printf("%d/%d",p,q);
        printf("\"\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/v5zsq/article/details/80407785