将石头分成质量相近的两组,并输出

背包问题,可以转化为0.1,递增排序好,选定两个,从最大的开始比较,给较小的加上第三大,以此循环,最后可以输出

#include <iostream>

#include<cstring>

#include<vector>

#include<algorithm>

using namespace std;

int main(){

    int n;

    vector<int> v; 

   while (cin >> n){

        cin.get();

        v.push_back(n);

    }

    sort(v.begin(),v.end());

    int sum=accumulate(v.begin(),v.end(),0);

    int l=v.size();

    int f=v[l-1],s=v[l-2];

    if(l>=3){

        for(int i=l-3;i>=0;--i){

            if(f>s)s+=v[i];

              

            else f+=v[i];

    }}

      

    if(f>s) cout<<f<<","<<sum-f;

    else cout<<s<<","<<f;

C++中编写石头、剪刀、布游戏的一个简单版本可以按照以下步骤进行: ```cpp #include <iostream> #include <ctime> // 引入时间库用于随机数生成 using namespace std; enum Choices { ROCK = 0, PAPER, SCISSORS }; // 定义三种选择 // 函数返回计算机的选择 Choices computerChoice() { srand(time(0)); // 初始化随机数种子 return (ROCK + PAPER + SCISSORS) % 3; // 计算机随机选择 } // 判断胜负 bool checkWin(Choices player, Choices computer) { if (player == computer) return false; // 平局 else if ((player == ROCK && computer == SCISSORS) || (player == PAPER && computer == ROCK) || (player == SCISSORS && computer == PAPER)) return true; else return false; } int main() { cout << "欢迎来到石头剪刀布游戏!\n"; int playerChoice; do { cout << "请输入你的选择(0: 石头, 1: 剪刀, 2: 布):"; cin >> playerChoice; // 检查输入是否有效 while (playerChoice < 0 || playerChoice > 2) { cout << "无效输入,请重新选择:"; cin >> playerChoice; } Choices computerChoice = computerChoice(); cout << "你选择了:" << (playerChoice == ROCK ? "石头" : playerChoice == PAPER ? "纸" : "布") << ", 计算机选择了:" << (computerChoice == ROCK ? "石头" : computerChoice == PAPER ? "纸" : "布") << endl; if (checkWin(playerChoice, computerChoice)) cout << "恭喜,你赢了!\n"; else if (checkWin(computerChoice, playerChoice)) cout << "很遗憾,电脑赢了!\n"; else cout << "平局!\n"; } while (true); // 如果玩家想要继续,循环会一直运行 return 0; } ``` 当你运行这个程序,它会提示用户输入他们的选择,并计算出计算机的选择。然后判断结果并给出相应的反馈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值