
1.加号运算符重载#include iostream using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours 0; minutes 0; } Time(int h, int m) { this-hours h; this-minutes m; } void show() { cout hours minutes endl; } Time operator (Time t2){ int hour this-hourst2.hours; int minute this-minutes t2.minutes; hour minute / 60; minute % 60; return Time(hour,minute); } }; int main() { int h, m; cin h; cin m; Time t1(h, m); Time t2(2, 20); Time t3 t1 t2; t3.show(); return 0; }2.重载⼩于号#include bits/stdc.h using namespace std; struct node { double w, x, y; }; int main() { int n; cin n; node tmp; for (int i 0; i n; i) { //n个学生 cin tmp.w tmp.x tmp.y; if (tmp.x * 7 tmp.y * 3 800 tmp.x tmp.y 140) //如果同时满足两个条件 cout Excellent\n; else //不满足 cout Not excellent\n; } return 0; }3.评等级#include bits/stdc.h using namespace std; struct Student{ char name[23]; int grade; }st[114];//存学生姓名成绩 bool cmp(Student A, Student B) { return A.grade B.grade; }//以成绩为关键字排序 int main () { int n; cin n; for(int i 1; i n; i) { cin st[i].grade; cin st[i].name; }//读入 sort(st 1, st n 1, cmp);//排序 cout st[1].name; return 0; }4.最⾼分数的学⽣姓名#include bits/stdc.h using namespace std; struct Student{ char name[23]; int grade; }st[114];//存学生姓名成绩 bool cmp(Student A, Student B) { return A.grade B.grade; }//以成绩为关键字排序 int main () { int n; cin n; for(int i 1; i n; i) { cin st[i].grade; cin st[i].name; }//读入 sort(st 1, st n 1, cmp);//排序 cout st[1].name; return 0; }5.甲流病⼈初筛#includebits/stdc.h using namespace std; int n,cough,ans; string name; //循环n次读入可以重复使用变量 double temp; int main() { cinn; for(int i1;in;i) { cinnametempcough; //读入病人的信息 if(temp37.5cough) //如果符合筛查条件 { coutnameendl; //输出名字 ans; //被初筛为甲流的病人数量1 } } coutansendl; //输出被初筛为甲流的病人数量 return 0; }6.找出前五名#include iostream #include algorithm using namespace std; int main() { int n; cin n; int a[55]; for (int i 0; i n; i) cin a[i]; // 从大到小排序 sort(a, a n, greaterint()); // 输出前五个 for (int i 0; i 5; i) { if (i 0) cout ; cout a[i]; } cout endl; return 0; }7.奖学⾦#include bits/stdc.h//万能头文件 using namespace std; struct xx{//定义结构体“学生”类型 int yw;//语文分数 int ss;//数学分数 int yy;//英语分数 int zf;//总分 int bh;//编号 }a[305];//可以直接定义注意定义结构体是后括号后一定要加上分号 int n; bool comp(xx x,xx y) { if(x.zf!y.zf) return x.zfy.zf; else if(x.yw!y.yw) return x.ywy.yw; else return x.bhy.bh; }//comp结构体排序上文已经解释过 int main() { cinn;//输入 for(int i1;in;i) { cina[i].ywa[i].ssa[i].yy;//输入第i位学生的语文、数学、英语 a[i].bhi;//存储学生的编号 a[i].zfa[i].ywa[i].ssa[i].yy;//定义总分 } sort(a1,an1,comp);//排序 for(int i1;i5;i) couta[i].bh a[i].zfendl;//循环输出 return 0; }8.⽣⽇#include bits/stdc.h using namespace std; int n; struct node { string s; int id, y, m, d; } a[110]; bool cmp(node a, node b) { if (a.y b.y) { if (a.m b.m) { if (a.d b.d) return a.id b.id; return a.d b.d; } return a.m b.m; } return a.y b.y; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin n; for (int i 1; i n; i) { cin a[i].s a[i].y a[i].m a[i].d; a[i].id i; } sort(a 1, a n 1, cmp); for (int i 1; i n; i) cout a[i].s \n; return 0; }