洛谷

游戏

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(0));
    char playAgain;

    do {
        // 游戏规则
        cout << "\n欢迎来到猜数字游戏!\n";
        cout << "系统已生成1-100之间的随机数,请开始猜测:\n";

        // 生成随机数
        int secretNumber = rand() % 100 + 1;
        int guess, attempts = 0;

        // 游戏主循环
        do {
            cout << "请输入你的猜测:";
            cin >> guess;
            attempts++;

            if (guess > secretNumber)
                cout << "猜大了!再试试\n";
            else if (guess < secretNumber)
                cout << "猜小了!再试试\n";
            else
                cout << "\n恭喜你猜对了!答案是 " << secretNumber 
                     << ",你用了 " << attempts << " 次\n";

        } while (guess != secretNumber);

        cout << "想再玩一次吗?(y/n): ";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    cout << "\n感谢游玩!再见!\n";
    return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    // 初始化随机数生成器
    srand(time(0));

    char playAgain;
    int playerChoice, computerChoice;
    int playerWins = 0, computerWins = 0;

    do {
        // 显示游戏菜单
        cout << "\n===== 石头剪刀布游戏 =====" << endl;
        cout << "1. 石头" << endl;
        cout << "2. 剪刀" << endl;
        cout << "3. 布" << endl;
        cout << "请选择 (1-3): ";
        cin >> playerChoice;

        // 电脑随机选择
        computerChoice = rand() % 3 + 1;

        // 显示双方选择
        cout << "\n你的选择: ";
        switch(playerChoice) {
            case 1: cout << "石头"; break;
            case 2: cout << "剪刀"; break;
            case 3: cout << "布"; break;
        }

        cout << "\n电脑选择: ";
        switch(computerChoice) {
            case 1: cout << "石头"; break;
            case 2: cout << "剪刀"; break;
            case 3: cout << "布"; break;
        }

        // 判断胜负
        cout << "\n结果: ";
        if (playerChoice == computerChoice) {
            cout << "平局!" << endl;
        } 
        else if ((playerChoice == 1 && computerChoice == 2) ||
                 (playerChoice == 2 && computerChoice == 3) ||
                 (playerChoice == 3 && computerChoice == 1)) {
            cout << "你赢了!" << endl;
            playerWins++;
        } 
        else {
            cout << "电脑赢了!" << endl;
            computerWins++;
        }

        // 显示比分
        cout << "比分: 你 " << playerWins << " - " << computerWins << " 电脑" << endl;

        // 询问是否再玩一次
        cout << "\n想再玩一次吗? (y/n): ";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    // 游戏结束
    cout << "\n游戏结束! 最终比分: 你 " << playerWins << " - " << computerWins << " 电脑" << endl;
    cout << "感谢游玩!" << endl;

    return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(0)); // 初始化随机数
    char playAgain;

    do {
        // 显示选项
        cout << "\n石头(1) 剪刀(2) 布(3):";
        int player, computer = rand() % 3 + 1;
        cin >> player;

        // 显示选择
        cout << "你选了";
        if (player == 1) cout << "石头";
        else if (player == 2) cout << "剪刀";
        else cout << "布";

        cout << ",电脑选了";
        if (computer == 1) cout << "石头";
        else if (computer == 2) cout << "剪刀";
        else cout << "布";

        // 判断结果
        if (player == computer)
            cout << ",平局!\n";
        else if ((player == 1 && computer == 2) || 
                 (player == 2 && computer == 3) || 
                 (player == 3 && computer == 1))
            cout << ",你赢了!\n";
        else
            cout << ",你输了!\n";

        cout << "再玩一次?(y/n):";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    cout << "游戏结束!\n";
    return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

// 简单计算器功能
void calculator() {
    double num1, num2;
    char op;

    cout << "\n=== 计算器 ===" << endl;
    cout << "请输入表达式(例如: 3 + 5): ";
    cin >> num1 >> op >> num2;

    switch(op) {
        case '+': cout << num1 << " + " << num2 << " = " << num1 + num2 << endl; break;
        case '-': cout << num1 << " - " << num2 << " = " << num1 - num2 << endl; break;
        case '*': cout << num1 << " * " << num2 << " = " << num1 * num2 << endl; break;
        case '/': 
            if(num2 != 0)
                cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
            else
                cout << "错误:除数不能为0!" << endl;
            break;
        default: cout << "错误:未知运算符!" << endl;
    }
}

// 随机数生成器
void randomNumberGenerator() {
    int min, max;

    cout << "\n=== 随机数生成器 ===" << endl;
    cout << "请输入范围 (最小值 最大值): ";
    cin >> min >> max;

    if(min >= max) {
        cout << "错误:最小值必须小于最大值!" << endl;
        return;
    }

    srand(time(0));
    int randomNum = rand() % (max - min + 1) + min;
    cout << "在 " << min << " 到 " << max << " 之间的随机数是: " << randomNum << endl;
}

// 猜数字游戏(计算机猜)
void numberGuesser() {
    cout << "\n=== 猜数字游戏 ===" << endl;
    cout << "请想一个1到100之间的数字,不要告诉我哦!" << endl;
    cout << "我会猜这个数字,你只需回答:" << endl;
    cout << "l - 我猜大了" << endl;
    cout << "s - 我猜小了" << endl;
    cout << "c - 猜对了" << endl;

    int low = 1, high = 100;
    int guess, attempts = 0;
    char feedback;

    do {
        guess = (low + high) / 2;
        attempts++;
        cout << "\n我猜是 " << guess << "?";
        cin >> feedback;

        if (feedback == 'l') high = guess - 1;
        else if (feedback == 's') low = guess + 1;

    } while (feedback != 'c');

    cout << "太棒了!我用了 " << attempts << " 次就猜对了!" << endl;
}

int main() {
    int choice;

    cout << "=== 我是你的计算机助手 ===" << endl;

    do {
        cout << "\n我可以为你做这些事情:" << endl;
        cout << "1. 计算器(加减乘除)" << endl;
        cout << "2. 随机数生成器" << endl;
        cout << "3. 猜数字游戏(我来猜)" << endl;
        cout << "4. 退出" << endl;
        cout << "请选择一项 (1-4): ";
        cin >> choice;

        switch(choice) {
            case 1: calculator(); break;
            case 2: randomNumberGenerator(); break;
            case 3: numberGuesser(); break;
            case 4: cout << "再见!" << endl; break;
            default: cout << "无效选择,请重试!" << endl;
        }

    } while (choice != 4);

    return 0;
}