C++Harry呀 2023-12-30 20:20 采纳率: 61.5%
浏览 3

c++一question十分火急的求help

求help一题,我的代码和题图如下

img

#include<bits/stdc++.h>
using namespace std;
int s1,s2,s3,x1,x2,x3;//s容量,x存量 
int main(){
    cin>>s1>>x1>>s2>>x2>>s3>>x3;
    int times=100;
    /*
    8 2
    6 3
    12 6*/
    while(times--){
//    cout<<"x1:"<<x1<<"\nx2:"<<x2<<"\nx3:"<<x3<<"\n";
        if(x1>0&&x2<s2){
            int trans=x1;
            x1-=trans;
            x2+=trans;
        }
        if (x2>0&&x3<s3) {
            int trans=x2;
            x2-=trans;
            x3+=trans;
        }
        if (x3>0&&x1<s1) {
            int trans=x3;
            x3-=trans;
            x1+=trans;
        }
        
    }
    cout<<x1<<"\n"<<x2<<"\n"<<x3;
}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-30 22:16
    关注

    【以下回答由 GPT 生成】

    #include <bits/stdc++.h>
    using namespace std;
    
    int s1, s2, s3, x1, x2, x3; // s容量,x存量
    
    int main(){
        cin>>s1>>x1>>s2>>x2>>s3>>x3;
    
        // 使用结构体存储参数
        struct State {
            int x;
            int s;
        };
    
        State state[3] = {{x1, s1}, {x2, s2}, {x3, s3}};
    
        int times = 100;
    
        // 模拟状态迁移
        while(times--){
            for(int i=0; i<3; i++){
                int j = (i+1)%3;
                if(state[i].x > 0 && state[j].x < state[j].s){
                    int trans = min(state[i].x, state[j].s - state[j].x);
                    state[i].x -= trans;
                    state[j].x += trans;
                }
            }
        }
    
        // 输出结果
        for(int i=0; i<3; i++){
            cout<<state[i].x<<"\n";
        }
    }
    


    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 12月30日