当前位置: 首页 > news >正文

C++-演讲比赛项目

需求

分析

1、初始化,生成演讲学生数组,打乱数组以便随机分组

2、每轮比赛后需要统计每个学生的胜场,以便决定进入下一轮和最终胜利的学生

代码实现

#pragma once#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>using namespace std;/*
* N为M的倍数
* N(=12)个学生参加演讲比赛。比赛共两轮。
* 第一轮、分组淘汰赛,随机分组,每组M(=6)人,两两演讲PK。每组决出前三名进入下一轮。
* 第二轮、决赛,每组两两PK,决出比赛前三名。
* 每轮比赛过后显示晋级选手信息。(可以保存比赛记录到文件中,方便查看)
*/
class Student {
public:int id;string name;Student(int id, string name) : id(id), name(name) {this->id = id;this->name = name;}int say() { // 演讲 通过比较随机数大小判断演讲胜负return rand() % 12;}
};static const int NUM = 12;
static const int MAX_NUM = 10001;
static Student** stu;
static Student** origin_stu;
static string s_name = "ABCDEFGHIJKL";auto int_asc_sort = [](int a, int b)->bool {return a > b;
};
auto asc_sort = [](pair<Student*, int> a, pair<Student*, int> b)->bool {return a.second > b.second;
};
static void print_set_no_erase(multiset<pair<Student*, int>, decltype(asc_sort)> topK) {for (multiset<pair<Student*, int>, decltype(asc_sort)>::iterator a = topK.begin(); a != topK.end(); a++) {pair<Student*, int> p = *a;Student* s = p.first;int count = p.second;cout << "id:" << s->id << " name:" << s->name << " 胜场: " << count << endl;}
}
static multiset<pair<Student*, int>, decltype(asc_sort)> print_set(multiset<pair<Student*, int>, decltype(asc_sort)> topK) {cout << "胜场降序排序后......" << endl;int limit = 1;for (multiset<pair<Student*, int>, decltype(asc_sort)>::iterator a = topK.begin(); a != topK.end(); a++) {if (limit++ > 6) {break;}pair<Student*, int> p = *a;Student* s = p.first;int count = p.second;cout << "id:" << s->id << " name:" << s->name << " 胜场: " << count << endl;}multiset<pair<Student*, int>, decltype(asc_sort)>::iterator a = topK.begin();a++; a++; a++;topK.erase(a, topK.end());return topK;
}
static void print_score(int a[], int len) {for (int i = 0; i < len; i++) {cout << s_name.substr(i, 1) << " 胜场: " << a[i] << endl;}
}
static multiset<pair<Student*, int>, decltype(asc_sort)> print_score(int a[], int len, multiset<pair<Student*, int>, decltype(asc_sort)> topK) {for (int i = 0; i < len; i++) {//cout << s_name.substr(i, 1) << " stu[" << i << "] = " << a[i] << endl;topK.insert(make_pair(origin_stu[i], a[i]));}return print_set(topK);
}
static void print_stu(Student ** stu, int len) {for (int i = 0; i < len; i++) {cout << "student[" << i << "] id:" << stu[i]->id << " name:" << stu[i]->name << endl;}
}// 初始化演讲的学生,打乱顺序以便随机分组
void init() {stu = new Student* [NUM];origin_stu = new Student* [NUM];srand(time(NULL));for (int i = 0; i < NUM; i++) {stu[i] = new Student(10000 + i + 1, s_name.substr(i, 1));origin_stu[i] = new Student(10000 + i + 1, s_name.substr(i, 1));}random_shuffle(stu, stu+NUM);//print_stu(stu);
}void competition(Student** stu1, Student** stu2, int len, int result[], int result2[]) {for (int i = 0; i < len; i++) {for (int j = 0; j < len; j++) {cout << "比赛:" << stu1[i]->name << " PK " << stu2[j]->name << " ";if (stu1[i]->say() >= stu2[j]->say()) {cout << stu1[i]->name << "胜" << endl;result[stu1[i]->id - MAX_NUM]++;}else{cout << stu2[j]->name << "胜" << endl;result2[stu2[j]->id - MAX_NUM]++;}}}
}// 提取哪些学生晋级了
void extraction(multiset<pair<Student*, int>, decltype(asc_sort)> top, Student** s) {int i = 0;for (multiset<pair<Student*, int>, decltype(asc_sort)>::iterator a = top.begin(); a != top.end(); a++) {s[i++] = a->first;}
}void merge_array(int a[], int b[], int c[], int len) {for (int i = 0; i < len; i++) {c[i] = a[i] + b[i];}
}void main_entry() {init();cout << "输入1开始第一轮比赛:";int start1;cin >> start1;if (start1 != 1) {cout << "游戏中止......" << endl;return;}Student** stu1 = new Student* [NUM/2];Student** stu2 = new Student * [NUM / 2];for (int i = 0; i < NUM; i++) {if (i < NUM/2) {stu1[i] = stu[i];}else {stu2[i-NUM/2] = stu[i];}}cout << "第一轮分组情况:" << endl;cout << "1组: " << endl;print_stu(stu1, NUM/2);cout << "2组: " << endl;print_stu(stu2, NUM / 2);delete[] stu;srand(time(NULL));int result[NUM] = {0};int result2[NUM] = {0};// 总共36场比赛 6 * 6 = 36// 0 7 8 9 10 11 12// 1 7 8 9 10 11 12// 2 7 8 9 10 11 12// 3 7 8 9 10 11 12// 4 7 8 9 10 11 12// 5 7 8 9 10 11 12// 6 7 8 9 10 11 12cout << "第一轮总共36场比赛,比赛结果如下:" << endl;competition(stu1, stu2, NUM/2, result, result2);//for (int i = 0; i < NUM / 2; i++) {//	for (int j = 0; j < NUM / 2; j++) {//		if (stu1[i]->say() >= stu2[j]->say()) {//			result[MAX_NUM - stu1[i]->id]++;//		}//		else//		{//			result2[MAX_NUM - stu2[j]->id]++;//		}//	}//}//print_score(result, NUM);//print_score(result2, NUM);int result_array1[NUM] = { 0 };merge_array(result, result2, result_array1, NUM);print_score(result_array1, NUM);//multiset<pair<Student*, int>, decltype(asc_sort)> topK(asc_sort);multiset<pair<Student*, int>, decltype(asc_sort)> set1(asc_sort);multiset<pair<Student*, int>, decltype(asc_sort)> set2(asc_sort);cout << "1组胜场详情:";multiset<pair<Student*, int>, decltype(asc_sort)> top1 = print_score(result, NUM, set1);cout << "2组胜场详情:";multiset<pair<Student*, int>, decltype(asc_sort)> top2 = print_score(result2, NUM, set2);cout << "1组前三名如下:" << endl;print_set_no_erase(top1);cout << "2组前三名如下:" << endl;print_set_no_erase(top2);delete[] stu1;delete[] stu2;cout << endl;cout << "---------------------------" << endl;cout << "输入数字1继续第二轮比赛:";int select;cin >> select;if (select != 1) {cout << "第二轮游戏中止......" << endl;return;}//sort(result, result + NUM, greater<int>());//sort(result, result + NUM, asc_sort);Student** s1 = new Student*[3];Student** s2 = new Student*[3];extraction(top1, s1);extraction(top2, s2);cout << "第二轮分组情况:" << endl;cout << "1组: " << endl;print_stu(s1, 3);cout << "2组: " << endl;print_stu(s2, 3);int r1[NUM] = {0};int r2[NUM] = {0};//int* r1 = new int[6]{0};//int* r2 = new int[6]{0};cout << "第二轮总共9场比赛,比赛结果如下:" << endl;competition(s1, s2, 3, r1, r2);//print_score(r1, NUM);//cout << endl;//print_score(r2, NUM);int result_array[NUM] = {0};merge_array(r1, r2, result_array, NUM);print_score(result_array, NUM);//sort(result_array, result_array + NUM, greater<int>());//sort(result_array, result_array + NUM, int_asc_sort);pair<Student*, int> p[NUM];for (int i = 0; i < NUM; i++) {p[i] = make_pair(origin_stu[i], result_array[i]);}sort(p, p+NUM, asc_sort);cout << "前三名信息如下:" << endl;cout << "第一名 id:" << p[0].first->id << " name:" << p[0].first->name << " 总胜场:" << p[0].second << endl;cout << "第二名 id:" << p[1].first->id << " name:" << p[1].first->name << " 总胜场:" << p[1].second << endl;cout << "第三名 id:" << p[2].first->id << " name:" << p[2].first->name << " 总胜场:" << p[2].second << endl;
}// 比赛入口
void entry() {while (true) {int select;cout << "输入1开始,0退出:" ;cin >> select;if (select) {system("cls");cout << "开始游戏..." << endl;cout << endl;main_entry();}else {break;}}cout << "比赛结束..." << endl;
}

相关文章:

  • 【机器人】复现 3D-Mem 具身探索和推理 | 3D场景记忆 CVPR 2025
  • 深度解析3D模型生成器:基于StyleGAN3与PyTorch3D的多风格生成工具开发实战
  • 【Web渗透】DVWA搭建详细教程
  • 用Python构建学生成绩管理系统的基本方案
  • leetcode hot100:解题思路大全
  • Web安全基础
  • 网络I/O学习-poll(三)
  • 异步记录用户操作日志
  • LLM笔记(九)KV缓存(2)
  • 智谱清言微服务架构转型实践——基于 CloudWeGo 的技术演进
  • 软件设计师“UML”真题考点分析——求三连
  • Triton介绍和各平台支持情况分析
  • 电路研究9.3.6——合宙Air780EP中的AT开发指南:FTP 应用指南
  • EMC风险评估详解
  • DApp开发全流程解析:模式设计、功能参考与合约管理实践
  • 【IDEA】删除/替换文件中所有包含某个字符串的行
  • 现阶段十个Agent协议概览
  • Redis学习打卡-Day4-Redis实现消息队列
  • React Flow 中 Minimap 与 Controls 组件使用指南:交互式小地图与视口控制定制(含代码示例)
  • ArcGIS Pro 3.4 二次开发 - 框架
  • 第1现场|俄媒称乌克兰网上出售北约对乌军培训手册
  • 苏州1-4月进出口总值增长6.8%,工业机器人出口额倍增
  • 习近平:坚持科学决策民主决策依法决策,高质量完成“十五五”规划编制工作
  • 淮安市车桥中学党总支书记王习元逝世,终年51岁
  • 蒲慕明院士:未来数十年不是AI取代人,而是会用AI的人取代不会用的
  • 下周或迎外贸“抢出口”高峰,跨境电商敏感货物如何便利化“登机”?