XTU 1236 Fraction
Fraction |
||
Accepted : 168 | Submit : 1061 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
FractionProblem Description:Everyone has silly periods, especially for RenShengGe. It's a sunny day, no is much more convient than 0.33333... as an example to support his So, RenShengGe lists a lot of numbers in textbooks and starts his great work. But RenShengGe is famous for his persistence, so he decided to sacrifice some InputThe first line contains a number T(no more than 10000) which represents the And there followed T lines, each line contains a finite decimal fraction x . OutputFor each test case, transform x in RenShengGe's rule. Sample Input3 Sample Output1/1 tipYou can use double to save x; |
#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 ;
}
最新文章
- jq封装淘宝图片轮播插件
- php对应js math.random
- P与NP问题
- IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法
- Data Base MongoVue 破解治标不治本
- css的使用技巧资料
- qt model/view 架构基础介绍之QTableWidget
- GitHub 常用命令使用介绍(新同学入门)
- Presto向分区表快速插入数据时出现&#39;target directory already exists&#39;的原因
- java表单重复提交常用解决办法
- H5 拖拽,一个函数搞定,直接指定对象设置可拖拽
- 关于CGI 和 PHP-FPM需要弄清的
- 【kafka学习之一】 kafka初识
- css中换行与不换行的样式
- [android] sharedPreference入门
- Redis数据";丢失";讨论及规避和解决的几点总结
- SIP SDP Profile-level-id解析
- python网络编程-optparse
- Codeforces 940F Machine Learning 带修改莫队
- python提纲