博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4180 扩展欧几里得
阅读量:5291 次
发布时间:2019-06-14

本文共 2519 字,大约阅读时间需要 8 分钟。

RealPhobia

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 376    Accepted Submission(s): 151

Problem Description
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.
 

 

Input
The 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 < B
 

 

Output
For 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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:          
 
 
 
#include
#include
long long gcd1(long long a,long long b,long long &x,long long &y){ if(b == 0) { x = 1; y = 0; return a; } long long d = gcd1(b,a%b,x,y); long long t = x; x = y; y = t - a/b*y; return d;}int main(){ int t; scanf("%d",&t); while(t--) { long long a,b; scanf("%lld/%lld",&a,&b); long long x = 0,y = 0; long long p = gcd1(a,b,x,y); //printf("%lld,%lld\n",x,y); //printf("---%lld\n",p); //printf("==%lld %lld\n",a,b); if(p != 1) { printf("%lld/%lld\n",a/p,b/p); continue; } if(a == 1) { printf("1/%lld\n",b-1); continue; } long long x1 = 0,y1 = 0; if(x > 0) { x1 = (a + y)%a; y1 = (b - x)%b; } else { x1 = (a - y)%a; y1 = (b + x)%b; } //printf("%lld %lld %lld %lld\n",x1,y1); printf("%lld/%lld\n",x1,y1); } return 0;}

 

转载于:https://www.cnblogs.com/13224ACMer/p/4729081.html

你可能感兴趣的文章
[leetcode]Valid Sudoku
查看>>
lesson 8:小程序
查看>>
鼠标悬停显示透明文字内容
查看>>
Window_Open详解
查看>>
centos使用--rpm和yum的关系以及基本用法
查看>>
PHP使用引用变量foreach时,切记其他循环不要使用同一个名字的变量
查看>>
第二类斯特林数总结
查看>>
随笔测试
查看>>
IIS Express 配置缓存位置
查看>>
单向链表
查看>>
Linux文件系统管理
查看>>
自己写的分页控件
查看>>
ContactsUtil 工具类 - 转载
查看>>
实验4-6:正弦动态圆
查看>>
docker 学习(五) virtualBox虚拟机安装docker
查看>>
Oracle企业管理框架
查看>>
HTML特效代码大全
查看>>
如何通过代码监控JVM的运行状态
查看>>
数据持久化的复习
查看>>
[YII2] COOKIE的操作使用
查看>>