Title DescriptionOn social media, description often see polls and results that agree on a particular point of view. For example, there are 1498 people who support a certain point of view and 902 against it, so the ratio of approval to opposition can be easily recorded as 1498:902.
However, if the findings are presented in this way, most people will not be satisfied. Because this ratio is too large, it is difficult to see their relationship at a glance. For the above example, if the proportion is recorded as 5:3, although the actual results have some error, but still can more accurately reflect the results of the survey, but also appear more intuitive.
The number of supporters is now given, against the number B, as well as an upper limit l, please reduce A to B to a ' than B ', require that a ' and B ' are not greater than L and a ' and B ' coprime (two integers greatest common divisor is 1), a '/b ' ≥a/b and a '/b '-A/b value as small as possible.
(This topic is 2014NOIP popularization T2)
input/output format input/output
Input Format:
Enter a total of three integer a,b,l, separated by a space between each of the two integers, representing the number of supporters, the number of opponents, and the upper limit, respectively.
output Format:
Output a total of one row, containing two integers a ', B ', separated by a space in the middle, indicating the scale after simplification.
input and Output sample sample Input/output
sample Test point # #
Input Sample:1498 902 10
Sample output:
5 3
idea: This problem can be used two-cycle, each time the two number to take greatest common divisor, Judge a '/b ' ≥a/b and a '/b '-A/b value as small as possible, if small, record I and J, continue to cycle, found there are smaller, records I and J, until the end, I and J is the answer.
The code is as follows:
1#include <stdio.h>2 intFunintXintY//x, y greatest common divisor3 { 4 if(y==0)returnx; 5 Else returnFun (y,x%y); 6 } 7 intMain ()8 { 9 intK;Ten inta,b,l;//Output One intA1,B1;//Answer A intI,j;//I and J for recycling - DoubleS1,s2,s3; -scanf"%d%d%d",&a,&b,&l); thes1=a*1.0/b;//a number that becomes a double -s3=l*1.0;//a number that becomes a double - for(i=1; i<=l;i++)//Enumerate L times - for(j=1; j<=l;j++) + if(Fun (i,j) = =1)//if greatest common divisor is 1, execution also - { +s2=i*1.0/j;//to be of type int, continue execution A if(S2>=S1&&S2-S1<S3)//a '/b ' ≥a/b and a '/b '-A/b value is as small as possible. at { -a1=i; -b1=J; -s3=s2-S1; - } - } inprintf"%d%d\n", A1,B1); - return 0; to}
Noip2014-Universal Group Rematch-second question-scale simplification