2024年码蹄杯决赛E题 小码哥的浇花选择 题型:思维 半独立完成

65 阅读1分钟

码题集OJ-小码哥的浇花选择 (matiji.net)

下面这样写过不了:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int flag;
int ans1, ans2;
signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
	int x, y, l; cin >> x >> y >> l;
	while (1)
	{
		int t = abs(x - l);
		int t2 = abs(y - l);

		if (l< t||l<t2)
		{
			cout << ans1 << " " << ans2 << endl;
			break;
		}
		if (flag == 0)
		{
			ans1 += t;
			flag = 1;
			l -= t;
		}
		else
		{
			ans2 += t2;
			flag = 0;
			l -= t2;
		}
	}
	return 0;
}

image.png

这样写才能过:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int flag;
int ans1, ans2;
signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
	int x, y, l; cin >> x >> y >> l;
	while (1)
	{
		if (flag == 0)
		{	
            int t = abs(x - l);
            if(l<t)
            {
                break;
            }
			ans1 += t;
			flag = 1;
			l -= t;
		}
		else
		{
            int t2 = abs(y - l);
            if(l<t2) break;
			ans2 += t2;
			flag = 0;
			l -= t2;
		}
	}
    cout<<ans1<<" "<<ans2<<endl;
	return 0;
}

image.png

image.png