lsmCC 发表于 2024-1-9 22:16:43

怎么修改

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
#define x first
#define y second
typedef pair<int,int>p;
int dx[]={-1,0,1,0},dy[]={0,-1,0,1}; //上左下右
void solve()
{
    int n,a,b;   // a为需要满足的水,b为马克杯的容量   
    int c=0,d=0;
    cin>>n>>a>>b;
    while(n--)
    {
      if(c==a)
      {
            c-=a;

      }
      else if(d==0)
      {
            d==b;
      }
      else
      {
            int ans=min(a-c,d);//表示要减到多少
            c+=ans;
            d-=ans;
      }
    }
    cout<<c<<" "<<d<<endl;

   
}
int main()
{
    int t;
    t=1;
    while(t--)
    {
      solve();
    }
}
当测试用例为
5 300 500
输出不是200 500 为什么一直是0 0 呢

FishC 发表于 2024-1-9 22:17:07

修改后的代码如下:

#include <iostream>
#include <algorithm>
using namespace std;

void solve()
{
    int n, a, b;
    cin >> n >> a >> b;

    int c = 0, d = 0;
    int ans = 0;

    while (n--)
    {
      if (c == a) // 如果水已经满了,就倒出去
      {
            c = 0;
            ans++;
      }
      else if (d == 0) // 如果马克杯空了,就加满
      {
            d = b;
            ans++;
      }
      else
      {
            int pour = min(a - c, d); // 需要倒的水量
            c += pour;
            d -= pour;
      }
    }

    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}
这段代码的作用是模拟一个操作,知道在满足特定条件时需要倒出多少次水才能完成操作。在每次循环中,根据当前水和马克杯状态判断应该进行什么操作,并更新水和马克杯的状态。最后输出需要倒出的次数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 怎么修改