XTU 1236 Fraction
来源:互联网 发布:cf英雄级武器淘宝网 编辑:程序博客网 时间:2024/06/06 07:27
Fraction
Accepted : 173 Submit : 1084Time 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 \\(1 \\over 3\\) 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\\(x \\in (0,1)\\).
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
解题思路:直接暴力枚举分母即可,从1~1000,题目也提示了输入用double
代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define INF 0x3f3f3f3f#define maxn 200005using namespace std;int gcd(int a,int b)///求最大公约数{ return !b ? a : gcd(b,a%b);}int main(){ int T; double n; scanf("%d",&T); while(T--) { scanf("%lf",&n); int x,y,t; double minn=100.0,num; for(int i=1; i<=1000; i++) { t=int(n*i+0.5); num=fabs(((double)t)/i-n); if(num<minn) { x=t,y=i; minn=num; } } int f=gcd(x,y); printf("%d/%d\n",x/f,y/f); } return 0;}
- XTU 1236 Fraction 二分
- XTU 1236 Fraction
- XTU 1236 Fraction(二分)
- XTU 1236 Fraction (二分)
- xtu 1236 Fraction(小数化分数)
- XTU 1236 Fraction(小数化分数)
- XTU 1236 Fibonacci
- Fraction
- Fraction
- xtu oj 1236 小数转分数
- goodbye, XTU
- XTU 1140
- XTU OJ
- XTU OJ
- XTU 1185
- xtu 4
- xtu 8
- POJ1930Dead Fraction
- javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG、javaCV-openCV)
- Python3.x 发送邮件
- JAVA中静态的使用及注意方法
- 严重: Allocate exception for servlet CustomerServlet java.lang.ClassNotFoundException: cn.itcast.cstm.
- Linux块设备驱动(一)————块设备的结构及磁盘的结构
- XTU 1236 Fraction
- Anconda 中Spyder无法打开IPython控制台
- javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
- Ubuntu16.04 下同时使用 Python2 和 Python3 及对应的 pip 的方法
- POJ3259_Wormholes_spfa
- RecyclerViewDemo
- javaCV开发详解之4:转流器实现(也可作为本地收流器、推流器,新增添加图片及文字水印,视频图像帧保存),实现rtsp/rtmp/本地文件转发到rtmp流媒体服务器(基于javaCV-FFMPEG)
- 安卓网络请求以及图片框架大集合
- CGridCtrl