1731: Scale Simplification
Time Limit: 1 Sec Memory Limit: 128 MBCommits: 260 Resolved: 106
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]
Topic description
On social media, it is common to see opinion polls and the results of whether you agree or disagree with a certain point of view. For example, there are 1498 people who support a certain point of view, and 902 people who disagree, so the ratio of approval and disapproval can be simply recorded as 1498:902.
However, if the survey results are presented in this way, most people will certainly not be satisfied. Because the value of this ratio is too large, it is difficult to see their relationship at a glance. For the above example, if the ratio is recorded as 5:3, although there is a certain error with the real results, it can still reflect the survey results more accurately, and it is also more intuitive.
Now given the number of supporters A, the number of opponents B, and an upper limit L, please reduce A to B to A' to B', requiring that both A' and B' are not greater than L and A' and B' are relatively prime (The greatest common divisor of two integers is 1), A'/B'≥A/B and the value of A'/B'-A/B is as small as possible.
However, if the survey results are presented in this way, most people will certainly not be satisfied. Because the value of this ratio is too large, it is difficult to see their relationship at a glance. For the above example, if the ratio is recorded as 5:3, although there is a certain error with the real results, it can still reflect the survey results more accurately, and it is also more intuitive.
Now given the number of supporters A, the number of opponents B, and an upper limit L, please reduce A to B to A' to B', requiring that both A' and B' are not greater than L and A' and B' are relatively prime (The greatest common divisor of two integers is 1), A'/B'≥A/B and the value of A'/B'-A/B is as small as possible.
enter
Enter a total of one line, including three integers A, B, L, separated by a space between each two integers, indicating the number of supporters, the number of opponents, and the upper limit. 1≤A≤1,000,000, 1≤B≤1,000,000, 1≤L≤100, A/B≤L.
output
The output is one line, including two integers A', B', separated by a space, indicating the simplified ratio.
sample input
1498 902 10
Sample output
5 3
It's hard to write~
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int gcd(int a, int b) { if(a > b) return gcd(b, a); if(b%a == 0) return a; else return gcd(a, b%a); } double abs(double n) return n < 0 ? -n : n; int main() { int A, B, L, result_a, result_b; while(cin >> A >> B >> L) { A /= gcd(A, B); B /= gcd(A, B); double shang = (double)A/B, a1 = L, b1 = L; for(int i=L; i>=1; i--) { for(int j=L; j>=1; j--) { if(gcd(i,j) != 1) continue; double i1 = i, j1 = j; if(i1/j1 >= shang && abs(i1/j1-shang) < abs(a1/b1-shang) { a1 = i; b1 = j; } } } cout << a1 << " " << b1 << endl; } } // 1498 902 10