P2118 比例简化

P2118 比例简化

题解

这题考虑暴力枚举,枚举1~L的两个数

反正数据也很小

代码

#include<bits/stdc++.h>

using namespace std;

int a,b,l,aa,bb;

int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}

int main()
{
    scanf("%d%d%d",&a,&b,&l);
    
    double t1,t2,t3=1e9;
    t1=(a*1.0)/(b*1.0);
    
    for(int i=1;i<=l;i++)
      for(int j=1;j<=l;j++)
      {
          if(gcd(i,j)==1)
          {
              t2=(i*1.0)/(j*1.0);
              if(t2>=t1&&t2-t1<t3)
              {
                  aa=i;
                  bb=j;
                  t3=t2-t1;
            }
        }
    
      }
    
    
    printf("%d %d",aa,bb);
    return 0;
}
原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/11096671.html