2015湖南湘潭第七届大学生程序设计比赛
D题

Fraction

Accepted : 133   Submit : 892
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  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

tip

You can use double to save x;

 
 

题意:输出最接近x的分数。分数的分母范围[1,1000]。
思路:打表求出所有的分数,注意0/1这个情况。二分求出分母。最好最后比较一下,因为输出的是最接近x的分数。
代码:

#include<bits/stdc++.h>
using namespace std;
#define eps 0.00000001
struct gg
{
double num;
int a,b;
} ans[];
int cmp(gg x,gg y)
{
return x.num<y.num;
}
int main()
{
int i,j,t;
t=;
ans[t].num=;
ans[t].a=;
ans[t++].b=;
for(i=; i<=; i++)
{
for(j=; j<i; j++)
{
if(__gcd(i,j)>) continue;
double x=j*1.0/i;
ans[t].num=x;
ans[t].a=j;
ans[t++].b=i;
}
}
ans[t].num=;
ans[t].a=;
ans[t++].b=;
sort(ans,ans+t,cmp);
int T;
double x;
scanf("%d",&T);
while(T--)
{
scanf("%lf",&x);
int l=,r=t,mid;
while(l<r)
{
mid=(l+r)/;
if(fabs(x-ans[mid].num)<eps) break;
if(x>ans[mid].num) l=mid+;
else r=mid;
}
int sign1=mid-,sign2=mid,sign3=mid+,sign;
double flag1=,flag2=,flag3=,flag;
if(sign1>=) flag1=ans[sign1].num;
if(sign2>=) flag2=ans[sign2].num;
if(sign3>=) flag3=ans[sign3].num;
if(fabs(flag1-x)<fabs(flag2-x)) sign=sign1;
else sign=sign2;
flag=ans[sign].num;
if(fabs(flag-x)>fabs(flag3-x)) sign=sign3;
printf("%d/%d\n",ans[sign].a,ans[sign].b);
}
return ;
}

2015湖南湘潭 D 二分的更多相关文章

  1. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  2. codeforces Looksery Cup 2015 H Degenerate Matrix 二分 注意浮点数陷阱

    #include <cstdio> #include <cstring> #include <algorithm> #include <string> ...

  3. noip2012~2015刷题小记录

    2012d1t1 密码 模拟题 #include<cstdio> #include<cstdlib> #include<cstring> #include<c ...

  4. 天气预报API(二):全球城市、景点代码列表(“旧编码”)

    说明 2016-12-10 补充 (后来)偶然发现中国天气网已经有城市ID列表的网页...还发现城市编码有两种,暂且称中国天气网这些编码为旧标准"旧编码"的特征是 9个字符长度; ...

  5. 人物-IT-刘强东:刘强东

    ylbtech-人物-IT-刘强东:刘强东 刘强东,男,汉族,1973年3月10日生(另一说法:1974年2月14日),江苏宿迁人,祖籍湖南湘潭 .京东集团董事局主席兼首席执行官,本科毕业于中国人民大 ...

  6. 基于SCADA数据驱动的风电机组部件故障预警

    吴亚联 1 , 梁坤鑫 1 , 苏永新 1* , 詹 俊 2(1.湘潭大学 信息工程学院, 湖南 湘潭 411105: 2.湖南优利泰克自动化系统有限公司, 湖南 长沙 410205) 摘 要: 为提 ...

  7. Android之控件使用

    Android系统为我们提供了大量的控件,例如:开关控件.单选按钮.多选按钮.单选菜单等等,那么这些控件如何使用呢?本篇我将带领大家一道学习一下如何使用这些控件.所谓无图无真相,先让大家看一下效果图: ...

  8. 时隔3年,再次折腾BlackBerry 8830!

    2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...

  9. Power Map

    推荐64位版本Office,但会遇到以下问题 建议大多数用户使用 32 位 Office 我们建议使用 32 版本的 Office,因为它与大多数其他应用程序更加兼容,尤其是第三方加载项.这也是默认安 ...

随机推荐

  1. Hash的一点测试

    哈希表的学习与测试 以前写的hash都是碰运气的hash,就是乘上质数取模的那种,这样不能保证不碰撞,所以今天先写上几个双hush和链表的hash,并比较一下他们的速度,测试的话用洛谷上的“字符串哈希 ...

  2. POI实现EXCEL单元格合并及边框样式

    POI实现EXCEL单元格合并及边框样式     下面例子为创建产生一个excel,合并单元格,然后为合并后的单元格添加边框 package test; import java.io.FileOutp ...

  3. 9.简单理解ajax

    #### post 请求需要发送一个header setRequestHeader('Content-Type','application/x-www-form-urlencoded') post请求 ...

  4. 完整版openlayer的例子及中文注释(完整中文版)

    //@sourceURL=PersonLocation.jsvar window_temp = { onbeforeunload: null, DEBUG_MODE: false, MAPLIST: ...

  5. centos配置DNS和ip

    Centos6.5 永久修改DNS地址的方法 1.配置ip地址文件 /etc/sysconfig/network-scripts/ifcfg-eth0添加一行 DNS1=8.8.8.8    #手动添 ...

  6. ubuntu16.04 安装opencv3.3

    from: http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/ Step #1: Install O ...

  7. Shiro权限总结

    参考学习地址   shiro 瞅完就会用(ssm+shiro)    Spring Shiro配置实现用户认证和授权 anon:它对应的过滤器里面是空的,什么都没做,另外.do和.jsp后面的*表示参 ...

  8. sqlserver分布式 用触发器插入数据

    这个月总公司收购了一家小公司,这家小公司的数据库用的是32位的 Sql2000 ,已经使用很长一段时间了,系统也比较稳定.本着节约成本的原则,总公司保留原公司的一套管理系统,但要求重要数据每天上传到总 ...

  9. JAVA中的异常疑点解析

    1 final, finally, finalize的区别. final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承. 内部类要访问局部变量,局部变量必须定义成final类型 ...

  10. scala -- 传名参数

    object Test{ def main(args: Array[String]): Unit = { def test(code : => Unit){// 传名参数 不计算函数值,而是把函 ...