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 ;
}

最新文章

  1. jq封装淘宝图片轮播插件
  2. php对应js math.random
  3. P与NP问题
  4. IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法
  5. Data Base MongoVue 破解治标不治本
  6. css的使用技巧资料
  7. qt model/view 架构基础介绍之QTableWidget
  8. GitHub 常用命令使用介绍(新同学入门)
  9. Presto向分区表快速插入数据时出现&#39;target directory already exists&#39;的原因
  10. java表单重复提交常用解决办法
  11. H5 拖拽,一个函数搞定,直接指定对象设置可拖拽
  12. 关于CGI 和 PHP-FPM需要弄清的
  13. 【kafka学习之一】 kafka初识
  14. css中换行与不换行的样式
  15. [android] sharedPreference入门
  16. Redis数据&quot;丢失&quot;讨论及规避和解决的几点总结
  17. SIP SDP Profile-level-id解析
  18. python网络编程-optparse
  19. Codeforces 940F Machine Learning 带修改莫队
  20. python提纲

热门文章

  1. 用 Python 写一个 NoSQL 数据库Python
  2. 大咖手把手教您,DLA一键建仓!
  3. 如何制作可以在 MaxCompute 上使用的 crcmod
  4. PHP配置环境中如何开启伪静态
  5. python中的True和False
  6. LintCode_469 等价二叉树
  7. springboot自定义错误页面(转)
  8. 深入浅出Javascript闭包
  9. 带三角形下标的提示框(按钮button)
  10. SpringBoot Actuator监控【转】