下面这样写过不了:
#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;
}
这样写才能过:
#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;
}