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 ...
随机推荐
- T1013 求先序排列 codevs
http://codevs.cn/problem/1013/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- java中的数据转换与前置,后置加加
public class Demo{ public static void main(String [] args){ int num=2; System.out.println(num++);//后 ...
- (转) go Cron的使用
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- 74.Search in a 2D Matrix
/* * 74.Search in a 2D Matrix * 12.5 by Mingyang * 这里面的对应挺巧的: * 这个就是将2D矩阵转化成1行数组的对应表.所以对于二分查找法的初始值为: ...
- Spring的IoC容器-Spring BeanFactory容器
虽然这个方法已经在3.1之后已经弃用了,但是可以了解一下. 在Spring中,有大量对BeanFactory接口的实现.其中,最常被使用的是XmlBeanFactory类.这个容器从一个XML文件中读 ...
- android 拍照预览
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- linxu下查看进程的线程方法;如何知道某个进程或者线程运行在哪个CPU上?
1.top -H -p <pid> ; top -H 在top命令后,按H键:或者top -H 2.ps -T -p <pid> “-T”选项可以开启线程查看 3.htop, ...
- 在windows Server 2008 R2server上使用infopath不能将表单公布到sharepoint server的问题处理。
在server 2008 R2 中.你将做好的表单公布到Sharepoint 时候会报错: 这个情况在client是2008 R2 Server 就会出现这个结果. 在角色中启用桌面体验就可以
- Java 实现原型(Prototype)模式
public class BaseSpoon implements Cloneable {//spoon 匙, 调羹 String name; public String getName() { re ...
- Linux 用户和文件权限管理
Linux —— 用户权限管理 权限: 为什么需要权限管理? 1.计算机资源有限,我们需要合理的分配计算机资源. 2.Linux是一个多用户系统,对于每一个用户来说,个人隐私的保护是十分重 ...