以下是使用 DEV C++ 5.11 版本编写的程序,根据饭量的大小输出 3 个人的顺序:

#include <iostream>
#include <vector>
#include <algorithm>

struct Person {
    char name;
    int correctness;

    bool operator<(const Person& other) const {
        return correctness > other.correctness; // 按照正确次数降序排序
    }
};

int main() {
    std::vector<Person> people {
        {'A', 0},
        {'B', 0},
        {'C', 0}
    };

    std::vector<std::string> statements {
        'B > A, C == A',
        'A > B, A > C',
        'C > B, B > A'
    };

    // 计算每个人说对的次数
    for (const auto& statement : statements) {
        for (auto& person : people) {
            int count = std::count(statement.begin(), statement.end(), person.name);
            if (count == person.correctness) {
                person.correctness++; // 如果说对了,增加正确次数
            }
        }
    }

    // 按照正确次数排序,输出饭量从小到大的顺序
    std::sort(people.begin(), people.end());

    std::cout << "按饭量从小到大的顺序是:";
    for (const auto& person : people) {
        std::cout << person.name << " ";
    }
    std::cout << std::endl;

    system("pause"); // 在 DEV C++ 中用于暂停控制台窗口

    return 0;
}

编译并运行这段代码,将输出按饭量从小到大的顺序:

按饭量从小到大的顺序是:B A C

注意:在 DEV C++ 5.11 版本中,使用 system("pause") 可以暂停控制台窗口,以便查看输出结果。如果你使用其他版本的编译器,可能需要适配不同的暂停方法。

C++ 程序:根据言语判断饭量大小

原文地址: https://www.cveoy.top/t/topic/bYY0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录