16.4【STL案例1-评委打分】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include<iostream> //#include<stdlib.h> #include<cstdlib> // 效果同上 using namespace std; #include<string> #include<vector> #include<deque> #include<algorithm> #include<ctime> /* 3.4 案例-评委打分 有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分最低分,取平均分。 1. 创建五名选手,放到vector中 2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中 3. sort算法对deque容器中分数排序,去除最高和最低分 4. deque容器遍历一遍,累加总分 5. 获取平均分 */ class Person { public : string name; int score; public : Person(string _name, int _score) { this ->name = _name; this ->score = _score; } }; void create_person(vector<Person> &v) { string name_seed = "ABCDE" ; for ( int i=0; i<5; i++) { string name = "选手" ; name += name_seed[i]; int score = 0; Person p(name, score); v.push_back(p); } } void set_score(vector<Person> &v) { for (vector<Person>::iterator it=v.begin(); it!=v.end(); it++) { deque< int > d; for ( int i=0; i<10; i++) { int score = rand () % 41 + 60; //60~100; rand()%40:0~39 d.push_back(score); } /* cout << "name:" << it->name << " score:" << endl; for(deque<int>::iterator dit=d.begin(); dit!=d.end(); dit++) { cout << *dit << " "; } cout << endl; */ sort(d.begin(), d.end()); //排序,默认升序 d.pop_back(); //去除最高分 d.pop_front(); //去除最低分 int sum = 0; for (deque< int >::iterator dit=d.begin(); dit!=d.end(); dit++) { sum += *dit; } int avg = sum / d.size(); // 求平均分 it->score = avg; } } void show_score(vector<Person> &v) { for (vector<Person>::iterator it=v.begin(); it!=v.end(); it++) { cout << "name:" << (*it).name << " score:" << (*it).score << endl; } } int main() { srand ((unsigned int ) time (NULL)); //真随机:随机数种子;防止每次运行打分都一样 vector<Person> v; create_person(v); /* for(vector<Person>::iterator it=v.begin(); it!=v.end(); it++) { cout << "name:" << (*it).name << " score:" << (*it).score << endl; } */ set_score(v); show_score(v); system ( "pause" ); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!