错误代码:
#include<bits/stdc++.h> using namespace std; struct person{ int k; int s; }a[10001]; bool cmp(person l,person b) { if(l.s == b.s){ if(l.k < l.s){ return false; } else{ return true; } } else if(l.s > b.s){ return true; } else{ return false; } } int main() { int m,n,t,sum = 0,fen; double m1; scanf("%d %d",&n,&m); for(int i = 1;i <= n;++ i){ scanf("%d %d",&a[i].k,&a[i].s); } sort(a + 1,a + n + 1,cmp); m1 = double(m) * 1.5; m = int(floor(m1)); fen = a[m].s; printf("%d",fen); for(int i = 1;a[i].s >= fen;++ i){ ++ sum; } printf(" %d",sum); for(int i = 1;a[i].s >= fen;++ i){ printf("\n%d %d",a[i].k,a[i].s); } return 0; }
主要是在写cmp函数的时候,没有想通这个到底是干啥的,导致排序的时候会出现混乱。其实就是检测分数相等时,返回序号小的,也就是按照序号小的排。
正确代码:
#include<bits/stdc++.h> using namespace std; struct person{ int k; int s; }a[10001]; bool cmp(person l,person b) { if(l.s != b.s){ return l.s > b.s; } else{ return l.k < b.k; } } int main() { int m,n,sum = 0,fen; double m1; scanf("%d %d",&n,&m); for(int i = 1;i <= n;++ i){ scanf("%d %d",&a[i].k,&a[i].s); } sort(a + 1,a + n + 1,cmp); m1 = double(m) * 1.5; m = int(floor(m1)); fen = a[m].s; printf("%d",fen); for(int i = 1;a[i].s >= fen;++ i){ ++ sum; } printf(" %d",sum); for(int i = 1;a[i].s >= fen;++ i){ printf("\n%d %d",a[i].k,a[i].s); } return 0; }
标签:return,1.10,05,int,sum,++,fen,person,分数线 From: https://www.cnblogs.com/Qwehhh-maimaiDX/p/17873382.html