Bert is a programmer with a real fear of floating point arithmetic. Bert has quite successfully used rational numbers to write his programs but he does not like it when the denominator grows large. Your task is to help Bert by writing a program that decreases the denominator of a rational number, whilst introducing the smallest error possible. For a rational number A/B, where B > 2 and 0 < A < B, your program needs to identify a rational number C/D such that: 
1. 0 < C < D < B, and 
2. the error |A/B - C/D| is the minimum over all possible values of C and D, and 
3. D is the smallest such positive integer.

InputThe input starts with an integer K (1 <= K <= 1000) that represents the number of cases on a line by itself. Each of the following K lines describes one of the cases and consists of a fraction formatted as two integers, A and B, separated by “/” such that: 
1. B is a 32 bit integer strictly greater than 2, and 
2. 0 < A < BOutputFor each case, the output consists of a fraction on a line by itself. The fraction should be formatted as two integers separated by “/”.Sample Input

3
1/4
2/3
13/21

Sample Output

1/3
1/2
8/13

题意:给定分数A/B,求C/D(满足D<B),使得C/D最接近A/B。

思路:可以用扩展欧几里德解决。这里是新认识了一种利用“连分数”来解决的做法。

连分数将A/B表示为一连续的分数=1/(B/A+B%A),然后B%A又继续递推,直到A=1,这时令B=B-1,然后带回这个连分数,就可以得到最接近的分数。

(如果是令B=B+1,得到的分数更接近A/B,但是不满足D<B。

(连分数还可以求把X开根号表示为分数。

#include<bits/stdc++.h>
using namespace std;
int a[],num;
void solve(int x,int y)
{
num=; int t;
while(x!=){
a[++num]=y/x;
t=x; x=y%x; y=t;
}
int C=,D=y-;
while(num>=){
t=D;D=a[num]*D+C;C=t;
num--;
}
printf("%d/%d\n",C,D);
}
int main()
{
int T,A,B,i,j;
scanf("%d",&T);
while(T--){
scanf("%d/%d",&A,&B);
int g=__gcd(A,B);
if(A==) printf("%d/%d\n",A,B-);
else if(g>) printf("%d/%d\n",A/g,B/g);
else solve(A,B);
}
return ;
}

HDU4188:RealPhobia (连分数的运用之一)的更多相关文章

  1. 连分数(分数类模板) uva6875

    //连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...

  2. 蓝桥杯 黄金连分数(BigDecimal的使用)

    标题: 黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜 ...

  3. 黄金连分数|2013年蓝桥杯B组题解析第四题-fishers

    黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜,它首次 ...

  4. 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】

    任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...

  5. 山科SDUST OJ Problem J :连分数

    Problem J: 连分数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2723  Solved: 801[Submit][Status][Web B ...

  6. SPOJ 3899. Finding Fractions 连分数

    连分数乱搞,我反正是一眼没看出结果 某巨巨把这题讲解的比较详细 : http://blog.csdn.net/gogdizzy/article/details/8727386 令k = [a/b] 然 ...

  7. [NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程

    题意:求方程x2-Dy2=1的最小正整数解 思路:用连分数法解佩尔方程,关键是找出√d的连分数表示的循环节.具体过程参见:http://m.blog.csdn.net/blog/wh2124335/8 ...

  8. java实现第四届蓝桥杯黄金连分数

    黄金连分数 题目描述 黄金分割数0.61803- 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜, ...

  9. [NOI2021] 密码箱 (平衡树,连分数,Stern-Brocot 树,矩阵)

    题面 记忆犹新 题解 f f f 函数值给得非常明显,一看就给人一种熟悉感--这不是连分数吗? 众所周知,连分数有个递推公式,即 p i = a i p i − 1 + p i − 2 q i = a ...

随机推荐

  1. SGU 106 在区间范围内的线性方程解个数

    题意:求解方程ax+by+c=0,在区间x1->x2和y1->y2的解的个数. 看似简单,真心a的不容易啊! 开始跪于第8组数据,原因是没用long long !后来改了,跪于12组,超时 ...

  2. Oracle外键级联删除和级联更新

    https://www.2cto.com/database/201507/417496.html

  3. Atcoder Grand Contest 023

    A 略 B 略 C(计数) 题意: 有n个白球排成一行,故有n-1个空隙,我可以给一个空隙对应的两个白球都涂黑.n-1个空隙的一个排列就对应着一个涂黑顺序,定义这个涂黑顺序的价值是“将所有n个球都涂黑 ...

  4. OO第三单元作业小结

    一.JML理论基础及应用工具链情况 理论基础 1.JML表达式 \result:表示方法执行后的返回值. \old(expr):表示一个表达式expr在相应方法执行前的取值. \foall:全称量词修 ...

  5. 12/10 C语言程序设计竞赛 后五题

    Title(题目) 小朋友顺逆报数 Problem ID(题目编号) (题目添加成功后由系统自动生成) Time Limit(运行时间限制) S(秒) Memory Limit(内存限制) MByte ...

  6. inux IO 内核参数调优 之 参数调节和场景分析

    http://backend.blog.163.com/blog/static/2022941262013112081215609/ http://blog.csdn.net/icycode/arti ...

  7. 新建JRapid项目(idea创建maven多模块项目)

    1.第一步,新建项目(Create New Project) 2.parent项目,不勾选“Crate from archetype”,直接单击“Next”. 3.groupid填写com.codin ...

  8. 使用Myeclipse + SVN + TaoCode 免费实现项目版本控制的详细教程

    通过Myeclipse + SVN插件 + TaoCOde可以省去代码仓库的租建:同时还可以很好的满足小团队之间敏捷开发的需求.接下来详细介绍整个搭建流程. 首先,介绍所用到的工具: 1,Myecli ...

  9. 一起学习CMake – 01

    一起学习CMake – 01 本节介绍CMake里最常用的三个命令,分别是cmake_minimum_required; project; add_executable等. CMake是个好东西,在使 ...

  10. es删除文档或者删除索引

    es删除文档或者删除索引 学习了:https://www.imooc.com/video/15771 删除文档: DELETE http://127.0.0.1:9200/people/man/1 删 ...