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. Idea配置Tomcat以及maven

    配置Tamcat Run-->Edit Configurations +-->如果列表里没有tomcat-->点击33 items more irrelevant继续寻找--> ...

  2. php执行超时(nginx,linux环境)

    与下面的参数有关 nginx: fastcgi_connect_timeout fastcgi_read_timeout fastcgi_send_timeout php-fpm:request_te ...

  3. Camtasia Studio录制屏幕字迹不清晰的原因

    Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...

  4. Java并发编程(三)volatile域

    相关文章 Java并发编程(一)线程定义.状态和属性 Java并发编程(二)同步 Android多线程(一)线程池 Android多线程(二)AsyncTask源代码分析 前言 有时仅仅为了读写一个或 ...

  5. Yii 清理缓存

    html: <button onclick="clearCache()">ClearCache</button> js: function clearCac ...

  6. Intel Active Management Technology

    http://en.wikipedia.org/wiki/Intel_Active_Management_Technology Intel Active Management Technology F ...

  7. 浅谈JavaScript的事件(事件对象)

    在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有关的信息.包括导致事件的元素.事件的类型和事件的相关信息.例如鼠标操作的事件中,会包含鼠标的位置信息.而键盘触发的 ...

  8. 【iOS系列】-自定义Modar动画

    [iOS系列]-自定义Modar动画.md 我们需要做的最终的modar动画的效果是这样的, 就是点击cell,cell发生位移,慢慢的到第二个界面上的.为了做出这样的动画效果,我们需要以下的知识. ...

  9. 标准代码书写 C++ 的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果 离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是 ...

  10. VC编码规范(转)

    1    项目风格   1.1    项目取名     在VC之中,项目名为最后可执行文件名,所以项目名最好以最终的可执行文件名一致. 1.2    项目目录设置     为保证VC项目的备份方便.快 ...