HDU4188:RealPhobia (连分数的运用之一)
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 (连分数的运用之一)的更多相关文章
- 连分数(分数类模板) uva6875
//连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...
- 蓝桥杯 黄金连分数(BigDecimal的使用)
标题: 黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜 ...
- 黄金连分数|2013年蓝桥杯B组题解析第四题-fishers
黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜,它首次 ...
- 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 ...
- 山科SDUST OJ Problem J :连分数
Problem J: 连分数 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2723 Solved: 801[Submit][Status][Web B ...
- SPOJ 3899. Finding Fractions 连分数
连分数乱搞,我反正是一眼没看出结果 某巨巨把这题讲解的比较详细 : http://blog.csdn.net/gogdizzy/article/details/8727386 令k = [a/b] 然 ...
- [NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程
题意:求方程x2-Dy2=1的最小正整数解 思路:用连分数法解佩尔方程,关键是找出√d的连分数表示的循环节.具体过程参见:http://m.blog.csdn.net/blog/wh2124335/8 ...
- java实现第四届蓝桥杯黄金连分数
黄金连分数 题目描述 黄金分割数0.61803- 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜, ...
- [NOI2021] 密码箱 (平衡树,连分数,Stern-Brocot 树,矩阵)
题面 记忆犹新 题解 f f f 函数值给得非常明显,一看就给人一种熟悉感--这不是连分数吗? 众所周知,连分数有个递推公式,即 p i = a i p i − 1 + p i − 2 q i = a ...
随机推荐
- Maven实现多个项目关联自动化构建(maven-invoker-plugin插件的使用)
以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_automation.html: 注意: ...
- CentOS7 docker.repo 用阿里云Docker Yum源
yum安装软件的时候经常出现找不到镜像的情况 https://download.docker.com/linux/centos/7/x86_64/stable/repodata/repomd.xml: ...
- [AngularJS + Unit Testing] Testing a component with requiring ngModel
The component test: describe('The component test', () => { let component, $componentController, $ ...
- c++中vector向量几种情况的总结(向量指针,指针的向量)
1.标准库vector类型 vector 是同一种类型的对象的集合.每一个对象都有一个相应的整数索引值.标准库将负责管理与存储元素相关的内存.我们把 vector 称为容器,是由于它能够包括其它对象. ...
- 一个能自己主动搜索源文件并自己主动推导的Makefile
今天看了一天的makefile的写法.东拼西凑.好不easy写出了一个makefile.颇有成就感,记录下来,以备温习之用. 如果有两个头文件文件夹 header1,header2;两个cpp文件文件 ...
- 133. Clone Graph (3 solutions)——无向无环图复制
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 【转载】图说OOP基础(一)
本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握. 涉及知识点: OOP的相关知识 OOP知识[Object-Orientation Programming 面向对象编程]总 ...
- HDU 1040 As Easy As A+B [补]
今天去老校区找她,不想带电脑了,所以没时间A题了 /*******************************************************************/ As Ea ...
- javascript读取和改动原型特别须要注意的事儿,由于原型的读写不具有对等性
对于从原型对象继承而来的成员,其读和写具有内在的不正确等性.比方有一个对象A,假设它的原型对象是B.B的原型对象是null.假设我们须要读取A对象的name属性值,那么JS会优先在A中查找.假设找到了 ...
- 小胖说事28------iOS中extern,static和const差别和使用方法
通俗的讲: extern字段使用的时候,声明的变量为全局变量,都能够调用,也有这样一种比較狭义的说法:extern能够扩展一个类中的变量到还有一个类中: static声明的变量是静态变量,变量值改变过 ...