比例简化

题目大意

现给出 AA,反对人数 BB,以及一个上限 LL,请你将 AABB 化简为 AA'BB',要求在 AA'BB' 均不大于 LLAA'BB' 互质(两个整数的最大公约数是 11)的前提下,A:BA':B'A:BA:BA:BA:BA':B'- A:B 的值尽可能小。

对于 100%100 \% 的数据,1A10000001 ≤ A ≤ 1000000, 1B10000001 ≤ B ≤ 1000000, 1L1001 ≤ L ≤ 100, A:BLA:B ≤ L

解题思路

仔细看 LL 的范围,其实这题对 AABB 进行简单枚举,然后判断是否互质,再根据题目判断就行了。

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;
}
posted @ 2021-09-20 14:27  蒟蒻orz  阅读(5)  评论(0编辑  收藏  举报  来源