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 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 [Math Processing Error]

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 [Math Processing
Error]

.

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;

 
 
 
看上去很复杂的题,其实是水题,不要被题目吓倒!
由于分母是1-1000,所以每次将所有的分母枚举一次,选接近的数就可以了。
 
题意:输入一个小数,输出最接近的分数,必须为最简分数。
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int gcd(int a,int b)
{
int c,t;
if(a<b)
{
t=a,a=b,b=t;
}
while(b)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int main()
{
int i,j,T;
double s,minn;
scanf("%d",&T);
while(T--)
{
scanf("%lf",&s);
int a=,b=;
minn=s;
for(i=; i<=; i++) //枚举1-1000的分母
{
j=s*i+0.5; //求出分子
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 ;
}

XTU 1236 Fraction的更多相关文章

  1. XTU1236 Fraction

    Fraction Accepted : 124 Submit : 806 Time Limit : 1000 MS Memory Limit : 65536 KB Fraction Problem D ...

  2. [LeetCode] Fraction to Recurring Decimal 分数转循环小数

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  3. Slave I/O: Got fatal error 1236

    [起因] 一次zabbix报警,从库[Warning] MySQL-repl was down  # 不知道主库/storage空间小于20%时为什么没有触发trigger [从库错误日志] 1611 ...

  4. BZOJ 1236: SPOJ1433 KPSUM

    Description 用+-号连接1-n所有数字的数位,问结果是多少. Sol 数位DP. \(f[i][j][0/1][0/1]\) 表示长度为 \(i\) 的数字,开头数字是 \(j\) ,是否 ...

  5. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  6. poj 1236 Network of Schools(连通图)

    题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...

  7. 1236 - Pairs Forming LCM -- LightOj1236 (LCM)

    http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...

  8. Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  9. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

随机推荐

  1. 高可用服务 AHAS 在消息队列 MQ 削峰填谷场景下的应用

    在消息队列中,当消费者去消费消息的时候,无论是通过 pull 的方式还是 push 的方式,都可能会出现大批量的消息突刺.如果此时要处理所有消息,很可能会导致系统负载过高,影响稳定性.但其实可能后面几 ...

  2. 如何解决Firefox浏览器地址栏中文搜索速度很慢

    一.插件安装 之前使用Chrome浏览器,习惯在地址栏中直接进行中文搜索.转到Firefox之后,突然发现在地址栏进行中文搜索,访问速度会很慢. 可以使用插件解决这个问题:Omnibar 插件地址:h ...

  3. CentOS8/RHEL8--恢复root用户密码及简易加固GRUB

    CentOS8/RHEL8--简易加固GRUB 今天突然想到放在数据中心的虚拟化平台下的Linux服务器,都是采用默认方式安装的,没有设置太多的安全选项,如果有恶意用户重启服务器后,通过GRUB调整启 ...

  4. go 文件操作 io

    package main import ( "fmt" "os" ) func main() { //打开文件 //概念说明: file 的叫法 //1. fi ...

  5. ip地址获取无效,自己修改ip地址

    (1)

  6. 简单介绍几个CSSReset的方法

    对于小型的网站来说,用这个并不会带来大的资源浪费,但如果是像Yahoo这种架构非常大的网站,必须要有选择地进行CSS重设,以减 少资源浪费. 正在使用CSS的你,用过CSS Reset吗?当然,或许你 ...

  7. DirectX11笔记(十一)--Direct3D渲染7--RENDER STATES

    原文:DirectX11笔记(十一)--Direct3D渲染7--RENDER STATES 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010 ...

  8. 【水滴石穿】ReactNativeDemo

    这个博主他的功底算是特别棒的了,能够把一些基础的例子,通过精巧的方式布局在一个小的demo里面 很值得我学习 放上博主的链接:https://github.com/jianiuqi/ReactNati ...

  9. bnd -buildpath指令的用法

    -buildpath的作用是为项目添加运行时依赖.这个依赖可以是workspace中的另一个项目或者是仓库中的另一个bundle. -buildpath指令只会在编译和构建时起作用,它从来不会被用来运 ...

  10. JavaScript--预解析在IE存在的问题

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...