50分求助!结构体排序找最接近比值
查看原帖
50分求助!结构体排序找最接近比值
301268
一只划水Doge楼主2021/2/19 16:54
/*
样例#2没过 但是下载的输入输出文件却与程序输出一样。结构体排序找最接近的值,bug找不到,求大佬解答。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a, b, l;
struct p
{
    int a, b;
    double ans;
    bool operator < (const p &a) const
    {
        return ans < a.ans;
    }
}p[105]; int cnt = 0;
int gcd(int x, int y)
{
    if (x % y == 0)
    {
        return y;
    }
    return gcd(y, x % y);
}
int main()
{
    cin >> a >> b >> l;
    for (int i = 1; i <= l; i++)
    {
      for (int j = 1; j <= l; j++)
     {
      if (gcd(i, j) == 1 && i * b >= j * a)
         {
              p[++cnt].a = i;
              p[cnt].b = j;
              p[cnt].ans = i * 1.0 / j;     
          }
      }
    }
    sort(p + 1, p + cnt+1);
    cout << p[1].a << " " << p[1].b << endl;
    return 0;
}
2021/2/19 16:54
加载中...