siyeansh @ 2021-08-31 18:27:44
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,ch[400],ma[400],en[400],score[400],num[400];
cin>>n;
for(int i=1;i<=n;i++){
cin>>ch[i]>>ma[i]>>en[i];
score[i]=ch[i]+ma[i]+en[i];
num[i]=i;
}
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(score[i]<score[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
}
else if(score[i]==score[j]&&ch[i]<ch[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
}
else if(score[i]==score[j]&&ch[i]==ch[j]&&num[i]>num[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
}
}
}
for(int i=1;i<=5;i++){
cout<<num[i]<<' '<<score[i]<<endl;
}
return 0;
}
by Sliarae @ 2021-08-31 18:57:34
@在下慕星尘 调好了
#include<bits/stdc++.h>
using namespace std;
int n,ch[400],ma[400],en[400],score[400],num[400];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>ch[i]>>ma[i]>>en[i];
score[i]=ch[i]+ma[i]+en[i];
num[i]=i;
}
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(score[i]<score[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
int c=ch[i];
ch[i]=ch[j];
ch[j]=c;
}
else if(score[i]==score[j]&&ch[i]<ch[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
int c=ch[i];
ch[i]=ch[j];
ch[j]=c;
}
else if(score[i]==score[j]&&ch[i]==ch[j]&&num[i]>num[j]){
int t=score[i];
score[i]=score[j];
score[j]=t;
int r=num[i];
num[i]=num[j];
num[j]=r;
int c=ch[i];
ch[i]=ch[j];
ch[j]=c;
}
}
}
for(int i=1;i<=5;i++){
cout<<num[i]<<' '<<score[i]<<endl;
}
return 0;
}
注意两点:
交换成绩和编号的同时也要交换语文成绩。
编号从小到大排序。
最后提示一下,交换两个变量的值可以用 swap(a,b)
语句。
by siyeansh @ 2021-08-31 18:59:24
@十里 回:多谢,我该如何报答你,我要以身相许!=~+~=
by lzymt @ 2021-08-31 20:14:28
@在下慕星尘 大了大了,格局大了