poj1079

1 . link address

https://vjudge.net/problem/POJ-1064#author=0

2 . Problem Description

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? 

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

3 . Problem-solving ideas

A given fraction, for instance 1498/902. When the determined denominator is 1, 2, .... as point of closest 1498/902.
When the denominator is 1, the closest 1498/902 score of 1/1.
When the denominator is 2, when the score was 3/2 closest 1498/902.
When the denominator is 3, when the score was 5/3 closest 1498/902.
And so on

4 . Algorithm source code

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    
    int den_init,num_init;  
    int den, num;  
    int num_temp;   
    int i = 0;
    while (cin >> num_init >> den_init)  
    {
        num = 1;  
        den = 0;  
 
        for (i = 1; i <= den_init; i++)   
        {
            num_temp = (int)((double)(i*num_init) / (double)den_init + 0.5); 
            if (den*abs(num_init*i - den_init*num_temp) < i*abs(num_init*den - den_init*num))
            {
                num = num_temp;
                den = i;
                cout << num << "/" << den << endl;
            }
            if (den_init*num == num_init*den) break;  
        }
        cout << endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/KasenBob/p/11221017.html