比例简化
题目大意
现给出 ,反对人数 ,以及一个上限 ,请你将 比 化简为 比 ,要求在 和 均不大于 且 和 互质(两个整数的最大公约数是 )的前提下,≥ 且 的值尽可能小。
对于 的数据,, , , 。
解题思路
仔细看 的范围,其实这题对 和 进行简单枚举,然后判断是否互质,再根据题目判断就行了。
AC CODE
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
double mmin = 9999999.0;
double kkk;
int ans1, ans2;
int gcd(int x, int y)
{
if(y == 0) return x;
return gcd(y, x % y);
}
signed main()
{
scanf("%lf%lf%lf", &a, &b, &c);
kkk = (double)a * 1.0 / (b * 1.0);
for(int i = 1; i <= c; ++i)
{
for(int j = 1; j <= c; ++j)
{
if(gcd(i, j) == 1)
{
if((double)i * 1.0 / (j * 1.0) >= kkk)
if(mmin > (double)abs(kkk * 1.0 - i * 1.0 / (j * 1.0)))
{
mmin = (double)abs(kkk * 1.0 - i * 1.0 / (j * 1.0));
ans1 = i;
ans2 = j;
}
}
}
}
printf("%d %d", ans1, ans2);
return 0;
}
本文来自博客园,作者:蒟蒻orz,转载请注明原文链接:https://www.cnblogs.com/orzz/p/18122125