本题目并不难,注意学习简约写法!!!
一个struct(),内涵函数重载简约代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, L, H;
struct Person
{
int id, moral, talent;
int level;
int total() const
{
return moral + talent;
}
bool operator< (const Person &t) const
{
if (level != t.level) return level < t.level;
if (total() != t.total()) return total() > t.total();
if (moral != t.moral) return moral > t.moral;
return id < t.id;
}
}p[N];
int main()
{
scanf("%d%d%d", &n, &L, &H);
int m = 0;
for (int i = 0; i < n; i ++ )
{
int id, moral, talent;
scanf("%d%d%d", &id, &moral, &talent);
if (moral < L || talent < L) continue;
int level;
if (moral >= H && talent >= H) level = 1;
else if (moral >= H && talent < H) level = 2;
else if (moral < H && talent < H && moral >= talent) level = 3;
else level = 4;
p[m ++ ] = {id, moral, talent, level};
}
sort(p, p + m);
printf("%d\n", m);
for (int i = 0; i < m; i ++ )
printf("%08d %d %d\n", p[i].id, p[i].moral, p[i].talent);
return 0;
}
三个struct(),冗余代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
struct P{
string name;
int l,w;//才华和德行
int sum=0;
}p[N],s[N],j[N],y[N],x[N];
int n,low,high;
bool cmp(P a,P b){
if(a.sum!=b.sum){
return a.sum>=b.sum;
}
else if(a.w!=b.w){
return a.w>=b.w;
}
else
return a.name<b.name;
}
int main(){
cin>>n>>low>>high;
int k=0;
int sp=0,jp=0,xp=0,yp=0;
while(n--){
char name[9];
int l,w;
scanf("%s%d%d",name,&w,&l);
if(l>=low&&w>=low){
k++;
int sum=l+w;
if(l>=high&&w>=high){
s[sp++]={name,l,w,sum};
}
else if(l<high&&w>=high){
j[jp++]={name,l,w,sum};
}
else if(l<high&&w<high&&w>=l){
y[yp++]={name,l,w,sum};
}
else{
x[xp++]={name,l,w,sum};
}
}
}
sort(s,s+sp,cmp);
sort(j,j+jp,cmp);
sort(y,y+yp,cmp);
sort(x,x+xp,cmp);
cout<<k<<endl;
for(int t=0;t<sp;t++){
printf("%s %d %d\n",s[t].name.c_str(),s[t].w,s[t].l);
}
for(int t=0;t<jp;t++){
printf("%s %d %d\n",j[t].name.c_str(),j[t].w,j[t].l);
}
for(int t=0;t<yp;t++){
printf("%s %d %d\n",y[t].name.c_str(),y[t].w,y[t].l);
} for(int t=0;t<xp;t++){
printf("%s %d %d\n",x[t].name.c_str(),x[t].w,x[t].l);
}
return 0;
}