暴力大法WA求助
查看原帖
暴力大法WA求助
409860
yu1102楼主2023/7/3 18:19

WA90分

思路:枚举分母,算出可能的分子并检查。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int n, m, a, b;
double x, r = 1e15;
bool tag;

void check(int i, int j)
{
	if (i > m) return ;
	
	if (b && LL(a) * j == LL(b) * i) return ;
	
	double nr = abs(x - i * 1.0 / j);
	if (nr < r)
	{
		a = i;
		b = j;
		r = nr;
		tag = 0;
	}
	else if (nr == r) tag = 1;
}

int main()
{
	cin >> m >> n >> x;
	for (int j = 1; j <= n; j++)
	{
		int i = floor(j * x);
		check(i, j);
		check(i + 1, j);
	}
	
	if (tag) puts("TOO MANY");
	else cout << a << '/' << b << endl;
}
2023/7/3 18:19
加载中...