码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 654 Ratio

时间:2014-07-10 23:01:17      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   strong   width   

Description

bubuko.com,布布扣


 Ratio 

If you ever see a televised report on stock market activity, you‘ll hear the anchorperson saysomething like ``Gainers outnumbered losers 14 to 9,‘‘ which means that for every 14 stocks thatincreased 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 ``Gainersoutnumbered 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 reporteda ratio of 14 to 9, which is 1.555555, for an error of 0.105199; he could have said ``5 to 3‘‘, andintroduced an error of only 1.666667-1.660754=0.005913. The estimate ``5 to 3‘‘ is not as accurateas ``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 ofeach number and use those as the approximate ratio.


What the anchorman needs is a list of rational approximations of increasing accuracy, so that he canpick one to read on the air. Specifically, he needs a sequence {bubuko.com,布布扣} where a1 is arational number with denominator 1 that most exactly matches the true ratio of winners to losers(rounding up in case of ties), ai+1 is the rational number with least denominator that provides amore accurate approximation than ai, and an is the exact ratio, expressed with the least possibledenominator. Given this sequence, the anchorperson can decide which ratio gives the best tradeoffbetween 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 isan improvement on the ratio 1/1, so neither would be considered. The best approximation withthree in the denominator 4/3, is more accurate than any seen so far, so it is one that should bereported. Finally, of course, 5/4 is exactly the ratio, and so it is the last number reported in thesequence.


Can you automate this process and help the anchorpeople?

Input 

The input file 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 pairis the number of gaining stocks for the day, and the second number is the number of losing stocksfor 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 ofgainers to losers. The first approximation has `1‘ as denominator, and the last is exactly the ratio ofgainers 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

题意:给了你a,b,让你在满足分母是1-b的序列成员,然后要求找出分子使得序列和a/b的差的绝对值越来越小

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
	int a, b, first = 1;
	while (scanf("%d%d", &a, &b) != EOF) {
		if (first)
			first = 0;
		else printf("\n");

		double cnt = 1.0*a/b, Min = 999999999.0;
		for (int i = 1; i <= b; i++) {
			int c = cnt*i+0.5;
			if (fabs(1.0*c/i-cnt) < Min-1e-7) {
				Min = fabs(1.0*c/i-cnt);
				printf("%d/%d\n", c, i);
			}
		}
	}
	return 0;
}



UVA - 654 Ratio,布布扣,bubuko.com

UVA - 654 Ratio

标签:des   style   http   color   strong   width   

原文地址:http://blog.csdn.net/u011345136/article/details/37600521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!