1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> // STL 使用相关的函数需要include进来 4 using namespace std; 5 struct Stu_info{ 6 char id[10]; 7 int de, cai, sum; 8 int flag; 9 }stu[100010]; 10 //此函数返回值为bool型,相应的返回 true 或者 false 11 bool cmp(Stu_info a, Stu_info b) { 12 if(a.flag != b.flag) return a.flag < b.flag; 13 else if(a.sum != b.sum) return a.sum > b.sum; 14 else if(a.de != b.de) return a.de > b.de; 15 else return strcmp(a.id, b.id) < 0; 16 } 17 int main(){ 18 int n, L, H; 19 scanf("%d%d%d", &n, &L, &H); 20 int m = n; 21 for(int i = 0; i < n; i++) { 22 scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai); 23 //总分计算 24 stu[i].sum = stu[i].de + stu[i].cai; 25 //判断类别,等地越高,分数越低 26 if(stu[i].de < L || stu[i].cai < L) { 27 stu[i].flag = 5; 28 m--; 29 } 30 else if(stu[i].de >= H && stu[i].cai >= H) stu[i].flag = 1; 31 else if(stu[i].de >= H && stu[i].cai < H) stu[i].flag = 2; 32 else if(stu[i].de >= stu[i].cai) stu[i].flag = 3; 33 else stu[i].flag = 4; 34 } 35 //通过cmp函数返回的值,来进行 (从大到小) 或者 (从小到大)的排序 36 sort(stu, stu + n, cmp); 37 // sort()见sort详解 38 printf("%d\n", m); 39 for(int i = 0; i < m; i++) { 40 printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai); 41 } 42 return 0; 43 } 44 45 46 bool compare(int a,int b){ 47 return a<b; //升序排列,如果改为return a>b,则为降序 48 } 49 int main(){ 50 int a[20]={2,4,1,23,5,76,0,43,24,65},i; 51 for(i=0;i<20;i++) 52 printf("%d", a[i]); 53 sort(a,a+20,compare); 54 for(i=0;i<20;i++) 55 printf("%d", a[i]); 56 return 0; 57 }
原文地址:https://www.cnblogs.com/zjsaipplp/p/10415848.html
时间: 2024-10-01 18:08:50