XTU 1236 Fraction(小数化分数)
来源:互联网 发布:windows版icloud是什么 编辑:程序博客网 时间:2024/05/17 03:37
Fraction
Accepted : 149 Submit : 988Time Limit : 1000 MS Memory Limit : 65536 KBFraction
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
思路:枚举分母,注意这里如果去枚举分子就会TLE。 这里有一个性质,分子=分母*结果的四舍五入值
即9/10=0.9 9≈10*0.9 1/9=0.111111111 1≈9*0.1111111111
代码:
#include <iostream>#include <math.h>#include<string.h>#include<algorithm>#include<stdio.h>using namespace std;const double eps = 1e-8;int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int main(){ int tcase; int a,b; scanf("%d",&tcase); while(tcase--){ double c; scanf("%lf",&c); double MIN = 999999; for(int i=1;i<=1000;i++){ int j = (int)(i*c+0.5); if(j<=i){ double d=j*1.0/i; ///当前答案 double e = fabs(c-d); if(MIN>e){ a = i; b = j; MIN = e; } if(MIN<eps) break; } if(MIN<eps) break; } int t = gcd(a,b); a = a/t,b = b/t; printf("%d/%d\n",b,a); } return 0;}
- xtu 1236 Fraction(小数化分数)
- XTU 1236 Fraction(小数化分数)
- POJ 1930 Dead Fraction (小数化分数)
- POJ 1930 Dead Fraction(小数化分数)
- XTU 1236 Fraction(二分)
- XTU 1236 Fraction (二分)
- hdu1717(小数化分数)
- XTU 1236 Fraction 二分
- XTU 1236 Fraction
- HDU1717(小数化分数,思维数学题)
- 小数化分数
- 小数化分数
- 小数化分数2
- 小数化分数
- 小数化分数
- 小数化分数2
- 小数化分数2
- 小数化分数
- GItHub----Makedown语法学习(快速入门)
- java json 变量所有的属性
- 建立一个对象模型的时候,为什么要重写hashCode与equals方法
- 嵌入式ARM多核处理器并行化方法
- HashTable和HashMap区别
- XTU 1236 Fraction(小数化分数)
- Fast RCNN训练自己的数据集 (2修改读写接口)
- quartz学习三--Spring集成
- MyEclipse配置Git每次push都要输入地址和密码的问题
- putty提示Network error:Software caused connection abort解决方法
- 多功能的PagerSlidingTabStrip
- git命令简写
- cross apply 与 inner join 的区别
- Javascript继承