siyuanaaa @ 2022-10-26 16:37:07
改了几次没有效果,测试点情况如下测试点
代码如下
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int n,chn[301],math,eng,score[301],sn[301];
cin>>n;
for(int i=1;i<=n;i++){
cin>>chn[i]>>math>>eng;
score[i]=chn[i]+math+eng;
sn[i]=i;
}
for(int i=1;i<=5;i++){
for(int j=i+1;j<=n;j++){
if(score[j]>score[i]){
int a=score[i];
score[i]=score[j];
score[j]=a;
a=sn[i];
sn[i]=sn[j];
sn[j]=a;
}else if(score[j]==score[i] && chn[j]>chn[i]){
int a=sn[i];
sn[i]=sn[j];
sn[j]=a;
}else if(score[j]==score[i] && chn[j]==chn[i] && sn[j]<sn[i]){
int a=sn[i];
sn[i]=sn[j];
sn[j]=a;
}
}
}
for(int i=1;i<=5;i++){
cout<<sn[i]<<" "<<score[i]<<endl;
}
return 0;
}
by Light_az @ 2022-10-26 16:43:15
交换时不能只交换一个变量,应将总分,学号,语文成绩一起调换
@siyuanaaa
by Light_az @ 2022-10-26 16:45:39
#include<iostream>
using namespace std;
int main(){
int n,chn[301],math,eng,score[301],sn[301];
cin>>n;
for(int i=1;i<=n;i++){
cin>>chn[i]>>math>>eng;
score[i]=chn[i]+math+eng;
sn[i]=i;
}
for(int i=1;i<=5;i++){
for(int j=i+1;j<=n;j++){
if(score[j]>score[i]){
int a=score[i];
score[i]=score[j];
score[j]=a;
a=sn[i];
sn[i]=sn[j];
sn[j]=a;
a=chn[i];
chn[i]=chn[j];
chn[j]=a;
}else if(score[j]==score[i] && chn[j]>chn[i]){
int a=score[i];
score[i]=score[j];
score[j]=a;
a=sn[i];
sn[i]=sn[j];
sn[j]=a;
a=chn[i];
chn[i]=chn[j];
chn[j]=a;
}else if(score[j]==score[i] && chn[j]==chn[i] && sn[j]<sn[i]){
int a=score[i];
score[i]=score[j];
score[j]=a;
a=sn[i];
sn[i]=sn[j];
sn[j]=a;
a=chn[i];
chn[i]=chn[j];
chn[j]=a;
}
}
}
for(int i=1;i<=5;i++){
cout<<sn[i]<<" "<<score[i]<<endl;
}
return 0;
}
by siyuanaaa @ 2022-10-26 17:04:14
@lianzhuo
十分感谢,这道题过了