问题解决策略数据类型实现训练4

张开发
2026/4/6 20:03:16 15 分钟阅读

分享文章

问题解决策略数据类型实现训练4
问题 A: 哪一天哪一秒题目描述小王特爱数字。期末复习各门功课烧脑厉害他想换换脑筋就想算算数。他想任意给出一个时间年月日时分秒得出是一年当中的第几天还有这是一年中的第几秒。下面的程序定义了一个结构体以及部分程序可以对n个时间做如上的操作请按给出程序限制的形式定义出其他必须的函数并提交。输入整数 nnn代表处理几个日期值nnn 行日期值每行的整数分别代表年、月、日、时、分、秒测试数据保证都是合法时间取值即不会出类似 222 月 303030 号的输入。输出nnn 行输出每行对应给出的时间是当年第几天以及是当年第几秒。输入输出样例样例输入 #1复制3 1971 1 8 14 25 48 2011 2 27 1 27 35 2012 11 2 11 23 1样例输出 #1复制8 656748 58 4930055 307 26479381提示按下面的框架完成#include stdio.h struct Time { int year; int month; int day; int hour; int minute; int second; }; void getTime(struct Time *pt); int dayOfYear(struct Time t); long secondOfyear(struct Time *pt); int main() { struct Time t; int i, n; scanf(%d, n); for (i 0; i n; i) { getTime(t); printf(%d %ld\n, dayOfYear(t), secondOfyear(t)); } return 0; } void getTime(struct Time *pt) { scanf(%d%d%d%d%d%d, pt-year, pt-month, pt-day, pt-hour, pt-minute, pt-second); return; }自行设计并提交剩余的部分#includeiostream #define int long long using namespace std; // 1 2 3 4 5 6 7 8 9 10 11 12 int month[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; bool is_leap_year(int y) { if (y % 400 0) return true; if (y % 4 0 y % 100 ! 0) return true; return false; } int day_of_month(int y, int m) { if (m 2 is_leap_year(y)) return 29; return month[m]; } int day_of_year(int y, int m, int d) { int day 0; int i 1; while (i m) { day day_of_month(y, i);//是第i月 i; } day d; return day; } int second_of_year(int days, int h, int m, int s) { int sum 0; sum (days - 1) * 24 * 60 * 60;//不包含这一天记得减一 sum h * 60 * 60; sum m * 60; sum s; return sum; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin n; while (n--) { int y, mo, d, h, m, s; cin y mo d h m s; int days day_of_year(y, mo, d); cout days ; cout second_of_year(days, h, m, s) \n; } return 0; }问题 B: 值日安排题目描述新生报到OJ 实验室新来了 777 名同学为了安排一周的宿舍值日情况决定按照年龄从小到大的顺序依次安排到周一至周日。请完善如下程序#include stdio.h #include stdlib.h struct studentInfo { int num; //学生学号 char name[20]; //学生姓名默认不超过20个字符 int year; //学生出生年份 int month; //学生出生月份 int day; //学生出生天 }; int main() { int i,j,k,tmp1,tmp2; //i,j迭代变量 char *days[7] {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}; struct studentInfo stu[7],tmpstu; //学生信息 for( i 0 ; i 7 ; i) scanf(%d %s %d %d %d,stu[i].num,stu[i].name,stu[i].year,stu[i].month,stu[i].day); /*******在下面填写代码***********/ /*******在上面填写代码***********/ for( i 0 ; i 7 ; i) //输出排好序的stu信息 printf(%s %d %s\n,days[i],stu[i].num,stu[i].name); return 0; }输入777 名同学的学号姓名出生年月日任意两个学生出生日期不同输出安排好的一周的值日表每行中的数据以空格隔开。输入输出样例样例输入 #1复制1224 lushaoxiao 1997 12 24 3132 wangxu 1997 11 10 3128 mengyuan 1997 10 1 3129 yushisheng 1997 8 12 1147 chenjueyu 1998 3 24 6125 lijianian 1998 6 14 3220 zhangliying 1997 7 18样例输出 #1复制Monday 6125 lijianian Tuesday 1147 chenjueyu Wednesday 1224 lushaoxiao Thursday 3132 wangxu Friday 3128 mengyuan Saturday 3129 yushisheng Sunday 3220 zhangliying提示只需提交需要填写部分的代码。for (i 0;i 7;i) { for (j 0;j 7 - i - 1;j) { int flag 0; if (stu[j].year stu[j 1].year) flag 1; else if (stu[j 1].year stu[j].year stu[j].month stu[j 1].month) flag 1; else if (stu[j 1].year stu[j].year stu[j 1].month stu[j].month stu[j].day stu[j 1].day) flag 1; if (flag 1) { tmpstu stu[j]; stu[j] stu[j 1]; stu[j 1] tmpstu; } } }问题 D: BAN-PICK题目描述在《第七史诗》的世界竞技场RTA中有这样的规则双方需提前禁用ban掉两个英雄然后轮流选择pick英雄至五个后再互相禁用ban对方的一个英雄以形成最后4V4的局面。所以一共需要禁用对方三个英雄现在Lonely 和 L老师 正在开房间玩RTA模拟战因为他们之间非常熟悉对方的图鉴box所以他们知道对方的英雄战斗力。当然禁用肯定是要禁对方战斗力强的英雄同时自己要选择自己能选择的战斗力强的英雄。你知道 Lonely 有 nnn 个英雄第 iii 个英雄的战斗力为 nin_ini​同时L老师 有 mmm 个英雄第 iii 个英雄的战斗力为 mim_imi​。 7≤n,m≤1007 \le n,m \le 1007≤n,m≤100 2000≤ni,mi≤2000002000 \le n_i,m_i \le 2000002000≤ni​,mi​≤200000。在双方完成 BAN PICK 阶段后请你给出在双方的决策均为最优的情况下双方最终出战的四个英雄的名称并预测本场战斗的可能结果。假设战斗的胜负由双方各自出战的英雄战斗力之和决定为了简化题目假设 Lonely 和 L老师 的图鉴完全不同即双方没有相同的英雄。保证不存在战斗力相同的英雄。输入第一行两个正整数 nnn 和 mmm表示 Lonely 和 L老师 分别拥有的英雄数量。 7≤n,m≤1007 \le n,m \le 1007≤n,m≤100接下来 nnn 行每行有两个数据分别表示 Lonely 的第 iii 个英雄的名称和战斗力 nin_ini​。接下来 mmm 行每行有两个数据分别表示 L老师 的第 iii 个英雄的名称和战斗力 mim_imi​。 2000≤ni,mi≤2000002000 \le n_i,m_i \le 2000002000≤ni​,mi​≤200000输出首先输出一行代表 Lonely 最终选择出战的四个英雄英雄与英雄名称之间存在一个空格要求按照战斗力从高到低的顺序输出。再输出一行代表 L老师 最终选择出战的四个英雄英雄与英雄名称之间存在一个空格要求按照战斗力从高到低的顺序输出。在第三行中如果 Lonely 获胜即选择的四个英雄战斗力之和大于 L老师则输出 “Lonely”反之输出 “L Teacher”。数据保证不会出现平局即双方战斗力之和相等的情况输入输出样例样例输入 #1复制7 7 Eda 102541 Tenebria 124511 Tywin 98551 Sharun 142468 Karina 143095 Luluka 143894 Lilias 144142 Violet 195041 Senya 133247 Shaow 58416 Rimuru 194875 Landy 151792 Winter 32114 Destina 199999样例输出 #1复制Sharun Tenebria Eda Tywin Landy Senya Shaow Winter Lonely提示Lonely 战斗力排前三的分别为KarinaLulukaLilias。所以 L老师 一定会BAN掉他们。Lonely会选出剩余英雄中战斗力排前四的即EdaTenebriaTywinSharun。Lonely的战斗力总和为10254112451198551142468468071。L老师 战斗力排前三的分别为VioletRimuruDestina。所以 Lonely 一定会BAN掉他们。则 L老师 会选择SenyaShaowLandyWinter出战。L老师的战斗力总和为1332475841615179232114375569。相比之下本场战斗 Lonely 的战斗力总和较高所以 Lonely 会获胜。#includeiostream #includevector #includestring #includealgorithm using namespace std; struct hero { string name; int val; }; bool cmp(struct hero a, struct hero b) { return a.val b.val; } int main() { int n, m; cin n m; vectorstruct herolon(n); vectorstruct herolyh(m); for (int i 0;i n;i) cin lon[i].name lon[i].val; for (int i 0;i m;i) cin lyh[i].name lyh[i].val; sort(lon.begin(), lon.end(), cmp); sort(lyh.begin(), lyh.end(), cmp); int cnt_lon 0; //最后只选四个英雄所以是i7不是in for (int i 3;i 7;i) { cnt_lon lon[i].val; cout lon[i].name ; } cout endl; int cnt_lyh 0; for (int i 3;i 7;i) { cnt_lyh lyh[i].val; cout lyh[i].name ; } cout endl; if (cnt_lon cnt_lyh) cout Lonely; else//注意L Teacher中间有空格 cout L Teacher; return 0; }问题 F: 谁挡住了我题目描述nnn 个人前后站成一列对于队列中的任意一个人如果排在他前面的人的身高大于等于他的身高则称该人被挡住了。小明是队列中的一员问有多少人挡住了他#include iostream using namespace std; struct Node { float height; Node *next; }; Node *createlist(int n) { Node *tnew Node; cint-height; if(n1) t-next createlist(n-1); else t-next NULL; return t; } Node *findlist(Node *head, int n) { if (n 1 || !head) return NULL; if (n 1) return head; return findlist(head-next, n-1); } int countlist(Node *head, Node *p) { if (!head || !p || head p) return 0; /* 请在该部分补充缺少的代码 */ } int main(void) { int n, pos; Node *head, *xiaoming; cin n; //人数 head createlist(n); cin pos; //小明的序号 xiaoming findlist(head, pos); cout countlist(head, xiaoming) endl; return 0; }输入第一行nnn第二行nnn 个人的身高第三行小明从前往后数的序号。输出挡住小明的人数。输入输出样例样例输入 #1复制10 1.86 1.74 1.67 1.87 1.68 1.9 1.65 1.65 1.68 1.65 8样例输出 #1复制7提示本题只需要提交填写部分的代码请按照C方式提交。int cnt 0; float target p-height;//注意这里是float不是int Node* cur head; while (cur ! p) { if (cur-height target) cnt; cur cur-next; } return cnt;问题 I 子序列问题线性表题目描述两个整数序列Aa1a2a3,...,am和Bb1b2b3,...,bn已经存入两个单链表中设计一个算法判断序列B是否是序列A的子序列代码给出如下请修改~本题只需提交修改部分#includestdio.h #includemalloc.h struct node //定义结构体 { int data; struct node *next; }; struct node *creat(int n) { struct node *head,*p,*q; //head是头节点p指向新开辟的节点q指向已连接的上一个节点 head(struct node *)malloc(sizeof(struct node));//给head开辟一个节点 q head; //q节点连接到head上 while(n--) //开辟n个新节点逐个连到链表上 { p(struct node *)malloc(sizeof(struct node)); scanf(%d,p-data);//p该连在q后边吧 q-next p; q p; } q-next NULL;//链表结束 return head; } void destroy(struct node *head) { struct node *p; while(head!NULL) { phead-next; delete(head); headp; } } int main()//建两条链表p,q { int m,n,count0; struct node *head1,*head2,*p,*q, *tmpq; scanf(%d,m); head1 creat(m); scanf(%d,n); head2 creat(n); q head1-next; p head2-next; while(q ! NULL) //双循环判断p是否是q的子列 { tmpq q; while(p!NULL) { /***修改代码******/ if(q-data p-data) { count; } else p p-next; /***********************/ } if(count ! n) { q tmpq - next; p head2 - next; count 0; } else break; } if(count n) printf(yes\n); else printf(no\n); destroy(p); destroy(q); return 0; }输入一个整数m表示A序列的长度m。m个数表示A序列中的m个数据元素。一个整数n表示B序列的长度n。n个数表示B序列中的n个数据元素。输出是的或者 不是输入输出样例样例输入 #19 12 13 14 15 6 71 18 19 10 5 15 6 71 18 19样例输出 #1yes这题我刚开始当成连续子序列做然后一直错。看了半天题目也没有发现什么猫腻题目的测试样例甚至都是连续子序列。没招了。当时认定了这题是连续子序列。想都不想就把pp-next改成了break认为这是故意写错的多余的代码毕竟这是改错题嘛。后来才发现这行代码并不是多余的。这次真的掉进固有思维了。题目真正意图判断B是否是A的非连续子序列if (p ! NULL q ! NULL q-data p-data) { count; p p-next; q q-next; } else p p-next;问题 K: C 语言习题 5.23 - 输出已交换后的两个值题目描述定义一个带参的宏或者模板函数带有三个参数第一个参数为类型后两个参数的值互换并写出程序输入两个数作为使用宏时的实参。输出已交换后的两个值。输入输入第一行有两个短整型数空格隔开输入第二行有两个小数空格隔开输入第三行有两个长整数空格隔开。输出交换后的两个数空格隔开。输入输出样例样例输入 #11 2 1.5 2.5 65535 2147483647样例输出 #12 1 2.5 1.5 2147483647 65535提示主函数已给定如下提交时不需要包含会自动添加到程序尾部。C:int main() { short int i1, i2; double d1, d2; long l1, l2; scanf(%hd%hd,i1, i2); SWAP(short int, i1, i2); printf(%hd %hd\n, i1, i2); scanf(%lf%lf, d1, d2); SWAP(double, d1, d2); printf(%g %g\n, d1, d2); scanf(%ld%ld, l1, l2); SWAP(long, l1, l2); printf(%ld %ld\n, l1, l2); return 0; }C:int main() { short int i1, i2; double d1, d2; long l1, l2; cin i1 i2; SWAP(short int, i1, i2); cout i1 i2 endl; cin d1 d2; SWAP(double, d1, d2); cout d1 d2 endl; cin l1 l2; SWAP(long, l1, l2); cout l1 l2 endl; return 0; }#includeiostream #define SWAP(type,a,b) swap(a,b);// using namespace std;下面这几个文件题都是之前的代码。问题 L: C语言习题5.25--文件操作1题目描述文本文件score.dic中存储了 nnn 名学生的信息班级编号姓名成绩每个学生信息占一行每行的数据之间使用制表符分割如下所示145811 fuxin 100 145811 chengxian 90 145812 zhangxue 92 145812 lijun 88 ......文件中存储的学生信息按照班级编号升序排列每个班级的人数可以不同要求读取文件中所有学生的成绩计算每个班级的平均成绩将班级编号和平均成绩输出。主程序已给出请根据提示补充缺失代码并提交该部分。#include stdio.h #include stdlib.h int main() { int num 0; /* 班级人数计数 */ int sumScore 0; /* 累计成绩*/ int curClass; /* 当前班级 */ int curScore; /* 当前成绩 */ int lastClass; /* 上一个班级*/ int readItems; /* 正确读入数据数目 */ FILE *fin; /* 输入文件 */ fin fopen(score.dic, r); /* 打开输入文件读 */ if (!fin) /* 文件打开失败 */ { fprintf(stderr, error open file!\n); /* 输出错误信息到标准错误设备 */ exit(-1); /* 强制退出并返回错误码 */ } /************************/ /* 读入班级和成绩 */ /* 读入数据合法 */ /* 处理连续的相同班级数据 */ /* 班级人数累计 */ /* 班级成绩累计 */ /* 读入下一个班级和成绩 */ /* 输出平均成绩 */ /* 数据清零 */ /* 关闭输入文件 */ /***********************/ return 0; }输入nnn 名学生的信息。班级编号姓名成绩文件读入输出每个班级的班级编号和平均成绩。样例输出复制145811 95 145812 90 ...提示打开文件时不需要指定路径注意实际答案数据多于样例char name[20]; fscanf(fin, %d\t%s\t%d, lastClass, name, sumScore); int cnt 1; while (fscanf(fin, %d\t%s\t%d, curClass, name, curScore)3) { if (curClass lastClass) { sumScore curScore; cnt; } else { printf(%d %d\n, lastClass, sumScore / cnt); cnt 1; sumScore curScore; lastClass curClass; } } printf(%d %d\n, lastClass, sumScore / cnt); fclose(fin); fin NULL;问题 M: 文件操作 - 二进制文件读入题目描述现有 100100100 名学生的姓名、学号、英语、数学、语文成绩存储在一个二进制文件student.dic中姓名用char[20]学号和各科成绩用int存储现要求将指定行数的学生信息输出每条信息占一行。前 5 行学生信息为akdh 13773 84 83 66 fjka 30257 15 14 88 sfhklas 61281 87 8 31 hfu 38635 55 50 60 iwehfk 92803 54 6 77输入要输出行号的整数序列以 000 作为结束标志。输出输出学生信息每个学生占一行。输入输出样例样例输入 #1复制1 3 5 0样例输出 #1复制akdh 13773 84 83 66 sfhklas 61281 87 8 31 iwehfk 92803 54 6 77提示打开文件时不需要指定路径。#include stdio.h #include stdlib.h #include ctype.h #includeerrno.h struct Student { char name[20]; int number; int chinese; int math; int english; }; int main() { FILE* pfRead fopen(student.dic, rb); if (pfRead NULL) { perror(fopen); exit(-1); } struct Student stu[100]; int i 0; for (i 0;i 100;i) { fread(stu[i], sizeof(struct Student), 1, pfRead); } int input 1; while (input) { scanf(%d, input); if(input0) break; printf(%s , stu[input-1].name); printf(%d ,stu[input-1].number); printf(%d , stu[input-1].chinese); printf(%d , stu[input-1].math); printf(%d\n, stu[input-1].english); } fclose(pfRead); pfRead NULL; return 0; }问题 N: 文件操作 - 文本文件读入题目描述现有 100100100 名学生的姓名、学号、英语、数学、语文成绩存储在一个文本文件student.dic中姓名不超过 202020 个字符学号和各科成绩为整型各数据之间用空格分隔现要求将指定行数的学生信息输出每条信息占一行。前 555 行学生信息为akdh 13773 84 83 66 fjka 30257 15 14 88 sfhklas 61281 87 8 31 hfu 38635 55 50 60 iwehfk 92803 54 6 77输入要输出行号的整数序列以 000 作为结束标志。输出输出学生信息每个学生占一行。输入输出样例样例输入 #1复制1 3 5 0样例输出 #1复制akdh 13773 84 83 66 sfhklas 61281 87 8 31 iwehfk 92803 54 6 77提示打开文件时不需要指定路径。#include stdio.h #include stdlib.h #include ctype.h #includeerrno.h struct Student { char name[20]; int number; int chinese; int math; int english; }; int main() { FILE* pfRead fopen(student.dic, r); if (pfRead NULL) { perror(fopen); exit(-1); } struct Student stu[100]; int i 0; for (i 0;i 100;i) { fscanf(pfRead, %s %d %d %d %d, stu[i].name, stu[i].number, stu[i].chinese, stu[i].math, stu[i].english); } int input 1; while (input) { scanf(%d, input); if (input 0) break; printf(%s , stu[input - 1].name); printf(%d , stu[input - 1].number); printf(%d , stu[input - 1].chinese); printf(%d , stu[input - 1].math); printf(%d\n, stu[input - 1].english); } fclose(pfRead); pfRead NULL; return 0; }问题 O: 英文字符大小写文件题目描述文本文件score.dic中存储了 nnn 名学生的信息班级编号姓名成绩每个学生信息占一行每行的数据之间使用制表符分割如下所示145811 fuxin 100 145811 chengxian 90 145812 zhangxue 92 145812 lijun 88 ...将文本文件score.dic中所有英文字母改成大写其它字符保持不变将结果输出。#include stdio.h #include stdlib.h #include ctype.h int main() { char curch, lastch; /* 当前读入字符上一次读入字符*/ FILE *fin; /* 输入文件 */ fin fopen(score.dic, r); /* 打开输入文件读 */ if (!fin) /* 文件打开失败 */ { fprintf(stderr, error open file!\n); /* 输出错误信息到标准错误设备 */ exit(-1); /* 强制退出并返回错误码 */ } /**************************/ /* 遍历输入文件的每一个字符 */ /* 当前字符是字母 */ /* 转换为大写并输出 */ /* 其它字符保持不变 */ /* 关闭输入文件 */ /*************************/ return 0; }主程序已给出请根据提示完成缺失部分并提交该部分。输入nnn 名学生的信息。班级编号姓名成绩文件读入输出经过变换后的学生信息。样例输出复制145811 FUXIN 100 145811 CHENGXIAN 90 145812 ZHANGQUE 92 145812 LIJUN 88这个文件题是自己现写的。就是把之前的作业改了几行就交上去了。测试运行的时候还显示无法打开文件吓哭孩子了结果第一次交上去就通过了笑死了。fscanf(fin, %c, lastch); while (fscanf(fin, %c, curch) 3) { if (lastch a lastch z) lastch A - a; printf(%c, lastch); lastch curch; } printf(%c, lastch); fclose(fin); fin NULL;问题 P: 趣味编程大赛题目描述猫猫学长带着今年的预备队员参加了sdibt大学第五届趣味编程大赛本次比赛共有九道题编号依次为A、B、C、D、E、F、G、H和I。比赛按照每人的做题数目和累计罚时进行综合排名。1做题数目多的排名高于做题数目少的2在做题数目相同的情况下按照罚时的累计时间进行排序罚时时间越少排名越高。其中罚时按照如下规则计算如果题目没有做对过则该题目不累计入总罚时。否则按照该题目第一次做对的时间累计入总罚时同时累计该题的做错次数每错一次增加20分钟罚时。例如zhangzhentang 同学的提交记录如下:A B C D E F G H I00:07:27(-1) 0(-2) 00:56:56(-4) 0(-3) 01:15:41(-1) 01:36:17(-1) 02:24:10(-1) 01:50:05(0) 0(-4)其中 A题提交的时间是00:07:27该题错过一次。B题没有做对但提交过两次错误......该同学的累计罚时计算为00:07:27 1*20分钟 00:56:56 4*20分钟 01:15:41 1*20分钟 01:36:17 1*20分钟 02:24:10 1*20分钟 01:50:05 10:50:36 。累计做对题目数 6 道题。输入n名同学的做题记录输出n名同学的排名以及罚时如果题目正确数和罚时完全相同则按照姓名的字典序排列。输入输出样例样例输入 #1Nick A B C D E F G H I linlin 00:07:21(-1) 0(0) 0(-3) 0(0) 00:41:17(0) 01:08:53(0) 02:29:25(-6) 03:17:39(-4) 0(0) ningnin 00:10:24(-3) 0(-4) 01:35:09(-7) 01:56:45(-2) 02:23:43(0) 0(0) 0(-4) 03:03:43(0) 0(0) feifei 00:06:58(-2) 0(0) 0(-6) 0(-6) 02:48:59(-9) 01:52:51(-4) 0(0) 02:18:42(0) 0(0) qiqi 00:49:07(-6) 0(0) 0(-1) 0(0) 01:36:37(-2) 02:10:25(-1) 0(-2) 0(-4) 0(0) xuanxua 00:12:56(-4) 0(-3) 0(-3) 01:16:41(0) 0(0) 0(-1) 0(-4) 0(-2) 0(0) juju 00:08:04(-2) 0(0) 01:13:06(-3) 0(0) 0(-1) 0(-3) 0(0) 0(0) 0(-2) jiajia 00:06:12(-1) 0(-1) 0(0) 0(0) 01:47:21(-3) 0(-2) 0(-2) 0(0) 0(0)样例输出 #1Rank Nick Solve Penalty 1 linlin 5 11:24:35 2 ningnin 5 13:09:44 3 feifei 4 12:07:30 4 qiqi 3 07:36:09 5 xuanxua 2 02:49:37 6 juju 2 03:01:10 7 jiajia 2 03:13:33提示Rank后有1个制表符Nick和Solve后均为2个制表符 有括号的数字输入可以用scanf(%d(%d))形式#define _CRT_SECURE_NO_WARNINGS #includeiostream #includecstdio #includestring #includevector #includealgorithm using namespace std; //Nick A B C D E F G H I //linlin 00:07 : 21(-1) //Rank Nick Solve Penalty //1 linlin 5 11:24 : 35 struct student { string name; int solve; int penalty; }; bool cmp(struct student a, struct student b) { if (a.solve b.solve) return true; if (a.solve b.solve) return false; if (a.solve b.solve a.penalty b.penalty) return true; if (a.solve b.solve a.penalty b.penalty) return false; if (a.solve b.solve a.penalty b.penalty) return a.name b.name; } int p_to_s(string s) { int ret 0; if (s 0) return 0; //01 2 34 5 67 //00 : 00 : 00 int h (s[0] - 0) * 10 (s[1] - 0); int m (s[3] - 0) * 10 (s[4] - 0); int se (s[6] - 0) * 10 (s[7] - 0); ret h * 3600 m * 60 se; return ret; } string s_to_p(int t) { string ans; if (t 0) { ans 0; return ans; } int h t / 3600; t - h * 3600; int m t / 60; t - m * 60; int s t; if (h 10) { ans.push_back(0); ans.push_back(h % 10 0); } else { ans.push_back(h / 10 0); ans.push_back(h % 10 0); } ans.push_back(:); if (m 10) { ans.push_back(0); ans.push_back(m % 10 0); } else { ans.push_back(m / 10 0); ans.push_back(m % 10 0); } ans.push_back(:); if (s 10) { ans.push_back(0); ans.push_back(s % 10 0); } else { ans.push_back(s / 10 0); ans.push_back(s % 10 0); } return ans; } void print_a_stu(struct student stu) { //注意这里用双引号不然name和solve会连一块。 cout stu.name \t\t stu.solve \t\t s_to_p(stu.penalty) endl; } void print_students(vectorstruct studentstu) { cout Rank\tNick\t\tSolve\t\tPenalty endl; int n stu.size(); int i 0; while (i n) { cout i 1 \t; print_a_stu(stu[i]); i; } } int main() { vectorstruct studentstu; string s; getline(cin, s); int c 0; while (getline(cin,s)) { c; string name; int n s.size(); int i 0; //注意这里不是数空格而是数制表符 while (i n s[i] ! \t) { name.push_back(s[i]); i; } i;//跳过\t int penalty 0; int solve 0; for (int j 0;j 9;j) { while (s[i] \t) i;//跳过\t string cost; while (i n s[i] ! () { cost.push_back(s[i]); i; } //cout cost cost endl; penalty p_to_s(cost); i;//跳过左括号 string tmp; while (s[i] ! )) { tmp.push_back(s[i]); i; } int cnt stoi(tmp) * (-1); //cout cnt cnt endl; if (cost ! 0) { solve; //注意这里是20分钟所以要乘60 penalty cnt * 20 * 60; //注意没做对的话错误次数不能算进罚时中所以要套一层if } i;//跳过右括号 } stu.push_back({ name,solve,penalty }); } sort(stu.begin(), stu.end(), cmp); print_students(stu); return 0; }

更多文章