Ratio
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4384 Accepted: 1616

Description
If you ever see a televised report on stock market activity, you’ll hear the anchorperson say something like ``Gainers outnumbered losers 14 to 9,’’ which means that for every 14 stocks that increased in value that day, approximately 9 other stocks declined in value. Often, as you hear that, you’ll see on the screen something like this:
Gainers 1498
Losers 902

As a person with a head for numbers, you’ll notice that the anchorperson could have said "Gainers outnumbered losers 5 to 3’’, which is a more accurate approximation to what really happened. After all, the exact ratio of winners to losers is (to the nearest millionth) 1.660754, and he reported a ratio of 14 to 9, which is 1.555555, for an error of 0.105199; he could have said `"5 to 3’’, and introduced an error of only 1.666667-1.660754=0.005913. The estimate "5 to 3’’ is not as accurate as “1498 to 902” of course; evidently, another goal is to use small integers to express the ratio. So, why did the anchorperson say ``14 to 9?’’ Because his algorithm is to lop off the last two digits of each number and use those as the approximate ratio.

What the anchorman needs is a list of rational approximations of increasing accuracy, so that he can pick one to read on the air. Specifically, he needs a sequence {a_1, a_2, …, a_n} where a_1 is a rational number with denominator 1 that most exactly matches the true ratio of winners to losers (rounding up in case of ties), a_{i+1} is the rational number with least denominator that provides a more accurate approximation than a_i, and a_n is the exact ratio, expressed with the least possible denominator. Given this sequence, the anchorperson can decide which ratio gives the best tradeoff between accuracy and simplicity.

For example, if 5 stocks rose in price and 4 fell, the best approximation with denominator 1 is 1/1; that is, for every stock that fell, about one rose. This answer differs from the exact answer by 0.25 (1.0 vs 1.25). The best approximations with two in the denominator are 2/2 and 3/2, but neither is an improvement on the ratio 1/1, so neither would be considered. The best approximation with three in the denominator 4/3, is more accurate than any seen so far, so it is one that should be reported. Finally, of course, 5/4 is exactly the ratio, and so it is the last number reported in the sequence.

Can you automate this process and help the anchorpeople?

Input
input contains several pairs of positive integers. Each pair is on a line by itself, beginning in the first column and with a space between the two numbers. The first number of a pair is the number of gaining stocks for the day, and the second number is the number of losing stocks for the day. The total number of stocks never exceeds 5000.

Output
For each input pair, the standard output should contain a series of approximations to the ratio of gainers to losers. The first approximation has ‘1’ as denominator, and the last is exactly the ratio of gainers to losers, expressed as a fraction with least possible denominator. The approximations in between are increasingly accurate and have increasing denominators, as described above.
The approximations for a pair are printed one to a line, beginning in column one, with the numerator and denominator of an approximation separated by a slash (``/’’). A blank line separates one sequence of approximations from another.

Sample Input
5 4
1498 902

Sample Output
1/1
4/3
5/4

2/1
3/2
5/3
48/29
53/32
58/35
63/38
68/41
73/44
78/47
83/50
88/53
93/56
377/227
470/283
563/339
656/395
749/451

Source
South Central USA 1998

问题链接:UVA654 LA5508 POJ1079 Ratio
问题简述:给定a和b,找出一个序列,要求分母i由1到b,分子u是所有可能的分子中u/i最接近a/b的,然后将中的一些项删除,保证所u/i是越来越接近a/b的。
问题分析:暴力枚举分母i,分子u = i * a /b(四舍五入),然后维护一个标准保证是接近的。
程序说明:程序在LA5508(UVALive5508)中出现WA,不理解。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA654 LA5508 POJ1079 Ratio */#include <iostream>
#include <cstdio>
#include <cmath>using namespace std;int main()
{int flag = 0, a, b;while(scanf("%d%d", &a, &b) != EOF) {if(flag++) printf("\n");double pre = 100000000.0;double p = 1.0 * a / b;for(int i = 1; i <= b; i++) {int u = i * p + 0.5;if(fabs(p - 1.0 * u / i) < pre) {pre = fabs(p - 1.0 * u / i);printf("%d/%d\n", u, i);}}}return 0;
}

UVA654 LA5508 POJ1079 Ratio【暴力】相关推荐

  1. 暴力视音频分类检测相关论文

    一   1993年 1993年的一篇Understanding andPreventing Violence指出暴力问题的严重性,作者以犀利的眼光和铮铮的事实数据表明美国社会充斥着暴力. 二   20 ...

  2. 暴力匹配与FLANN

    1.暴力匹配与FLANN的匹配时间比较 当ORB特征点为1000时,FLANN慢于暴力匹配,当ORB特征点>=1500时,FLANN时间少于暴力匹配. 特征点数目1000:(以下第一是FLANN ...

  3. 喵哈哈村的魔法考试 Round #1 (Div.2) 题解源码(A.水+暴力,B.dp+栈)

    A.喵哈哈村的魔法石 发布时间: 2017年2月21日 20:05   最后更新: 2017年2月21日 20:06   时间限制: 1000ms   内存限制: 128M 描述 传说喵哈哈村有三种神 ...

  4. 哑谜,回文和暴力之美

    暴力搜索是一个有趣的东西.至少刘汝佳是这么认为的.编程之美的4.10节就是典型的暴力题.虽然作者将其难度定义为一颗星,但却不能因此认为这个类型的问题就是那么容易的,很多可能需要一些有创造力的想法. 不 ...

  5. Leetcode 15.三数之和 双指针 or 暴力哈希

    题目链接:传送门 题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组. 暴力+ ...

  6. HDU - 5875 2016 ACM/ICPC 大连网络赛 H题 暴力

    题目链接 题意:给你一个区间l,r一直将val[l]模上val[l+1],val[l+2]...val[r],因为一个模上比前一个数小数是没有意义的,所以需要将每一个点找到右边第一个小于他的点就行. ...

  7. 【OpenCV】图像/视频相似度测量PSNR( Peak signal-to-noise ratio) and SSIM,视频/图片转换

    目录 1 目标 2 原理 2.1 图像比较 - PSNR and SSIM¶ 3 代码 3.1如何读取一个视频流(摄像头或者视频文件)?¶ 3 运行效果 视频/图片转换: 如何用OpenCV创建一个视 ...

  8. 2015湖南省省赛 阶乘除法 暴力

    阶乘除法 Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: NBUT 1643 Description 输入两个正整数 n ...

  9. 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由SQL GM 发表于云+社区专栏 介绍 对于SSH服务的常见的攻击就是暴力破解攻击--远程攻击者通过不同的密码来无限次地进行登录尝试. ...

最新文章

  1. Tcpdump源码分析系列7:main函数
  2. python3 ftplib_ftplib — FTP protocol client
  3. oracle复合索引介绍(多字段索引)
  4. clientHeight,offsetHeight,scrollHeight迷一样的三个值
  5. R学习-小白笔记05
  6. java string sscanf_倾情奉献——JAVA sscanf函数!!!
  7. java 时间 转化成数字_java时间转化数字
  8. TCP发送端突发性(burst)发送报文的成因
  9. 如何利用用户ID号、关键词或视频时长在自媒体视频软件上批量采集下载关于在自媒体视频软件上批量采集...
  10. igs游戏linux模拟器,IGS经典游戏FBAS模拟器典藏版
  11. java xml特殊字符_mybatis xml中特殊字符处理及特殊符号
  12. cups ipp oracle,CUPS cups/ipp.c空指针引用拒绝服务漏洞
  13. 数据分析出的 2000年以来高分华语电影前50名
  14. WIN7卸载IE11回复IE8的方法
  15. ToxinPred – 多肽毒性预测、突变设计和理化性质预测
  16. Vue3+ElementPlus网页端聊天|vue3.0仿微信/QQ界面实例
  17. checking for gcc... no
  18. chuwi hi8 android 5,驰为Hi8,玩够了安卓还能办办公?
  19. 半导体器件(不定时更新)
  20. 平安科技java_平安科技面试整个过程!

热门文章

  1. [整理] TPM 2.0 设备串口通讯协议中文文档
  2. 如何开发Web3D游戏
  3. Nginx路由--Location 的使用
  4. js判断html存不存在某个属性,javascript如何判断对象是否存在某属性?
  5. Zookeeper集群部署及报错分析
  6. Python 之 变量进阶(理解)
  7. java有关问题,Java常见有关问题和解决方法
  8. java中$和 的区别详解_MyBatis中#{}和${}的区别详解
  9. vb 复制 剪贴板 html,VB把选中的内容复制到剪切板
  10. 小型英语字典(字典训练)