24h購物| | PChome| 登入
2013-12-15 19:32:48| 人氣1,391| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 654 - Ratio

推薦 0 收藏 0 轉貼0 訂閱站台


  Ratio 

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, dots, a_n$} where a1 is a rational 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 a more accurate approximation than ai, and an 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 

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



Miguel A. Revilla
2000-01-17

最後還是參照網路上的解法,說實在看不懂題目在描述什麼。
總之,每次用最小的分母,依序逼近原本的比例,逼近可以超過也可以少於。

一開始一定從分母為 1 找到最接近的比例形式。
接著依序窮舉找到下一個分母的最接近型式,其差值比前一個還來得小。

#include <stdio.h>
struct ratio {
    long long a, b;
};
int checkin(ratio a, ratio b, ratio c) {
    long long r1, r2;
    r1 = a.a * b.b - a.b * b.a;
    r2 = b.a * c.b - b.b * c.a;
    r1 *= c.b;
    r2 *= a.b;
    if(r1 < 0)    r1 = -r1;
    if(r2 < 0)    r2 = -r2;
    return r1 < r2;// |a-b| < |b-c|
}
int main() {
    ratio r;
    int cases = 0;
    while(scanf("%lld %lld", &r.a, &r.b) == 2) {
        if(cases++)    puts("");
        ratio prev, i;
        for(i.b = 1; i.b <= r.b; i.b++) {
            i.a = (r.a * i.b + r.b/2) / r.b;
            if(i.b == 1 || checkin(i, r, prev)) {
                printf("%lld/%lld\n", i.a, i.b);
                prev = i;
            }
        }
    }
    return 0;
}

台長: Morris
人氣(1,391) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][bfs] 633 - A Chess Knight
此分類上一篇:[UVA][dp] 11400 - Lighting System Design

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文