AcWing每日一题--比例简化

https://www.acwing.com/problem/content/460/

发现L的范围只有100,直接枚举分子分母。

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int gcd(int a,int b){
 5     if(b==0){
 6         return a;
 7     }
 8     return gcd(b,a%b);
 9 }
10 int main(void){
11     int a,b,l;
12     cin>>a>>b>>l;
13     double sub=1e9;
14     int resa,resb;
15     for(int i=0;i<=l;i++){
16         for(int j=1;j<=l;j++){
17             double t1=double(a)/b;
18             double t2=double(i)/j;
19             if(t2-t1>=0&&t2-t1<sub){
20                 resa=i,resb=j,sub=t2-t1;
21             }
22         }
23     }
24     int t=gcd(resa,resb);
25     resa/=t,resb/=t;
26     cout<<resa<<" "<<resb;
27     return 0;
28 }
原文地址:https://www.cnblogs.com/greenofyu/p/14396724.html