PAT甲级 1062 Talent and Virtue 排序+模拟 📅 2026/7/28 16:19:21 代码如下//排序#includeiostream#includestring#includealgorithm#includevectorusing namespace std;structpeople{intid;intv_grade;//美德成绩intt_grade;//智力成绩inttotal;//总成绩};vectorpeoplesage;//圣人vectorpeoplenobleman;//君子vectorpeoplesmallman;//小人vectorpeoplefoolman;//愚人intn;intans0;//有效人数intlow,high;boolcmp(people a,people b){if(a.totalb.total){returntrue;}elseif(a.totalb.total){if(a.v_gradeb.v_grade){returntrue;}elseif(a.v_gradeb.v_grade){returna.idb.id;}}returnfalse;}intmain(){cinnlowhigh;for(inti0;in;i){people temp;cintemp.idtemp.v_gradetemp.t_grade;temp.totaltemp.v_gradetemp.t_grade;intvtemp.v_grade,ttemp.t_grade;if(vlow||tlow){continue;}elseif(vhighthigh){ans;sage.push_back(temp);}elseif(vhighthigh){ans;nobleman.push_back(temp);}elseif(vhighthighvt){ans;foolman.push_back(temp);}else{ans;smallman.push_back(temp);}}sort(sage.begin(),sage.end(),cmp);sort(nobleman.begin(),nobleman.end(),cmp);sort(foolman.begin(),foolman.end(),cmp);sort(smallman.begin(),smallman.end(),cmp);coutansendl;for(inti0;isage.size();i){coutsage[i].id sage[i].v_grade sage[i].t_gradeendl;}for(inti0;inobleman.size();i){coutnobleman[i].id nobleman[i].v_grade nobleman[i].t_gradeendl;}for(inti0;ifoolman.size();i){coutfoolman[i].id foolman[i].v_grade foolman[i].t_gradeendl;}for(inti0;ismallman.size();i){coutsmallman[i].id smallman[i].v_grade smallman[i].t_gradeendl;}return0;}