caimozeng044 @ 2023-07-27 11:04:13
#include <bits/stdc++.h>
using namespace std;
int n;
struct stu{
int chinese;
int math;
int english;
int score;
int num;
};
int main()
{
cin>>n;
stu a[301],sum[301];
for(int i=1;i<=n;i++){
cin>>a[i].chinese>>a[i].math>>a[i].english;
sum[i].score=a[i].chinese+a[i].math+a[i].english;
sum[i].num=i;
}
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
if(sum[i].score<sum[j].score){
swap(sum[i].score,sum[j].score);
swap(sum[i].num,sum[j].num);
}
else if(sum[i].score==sum[j].score){
if(a[i].chinese<a[j].chinese){
swap(sum[i].score,sum[j].score);
swap(sum[i].num,sum[j].num);
}
}
}
}
for(int i=1;i<=5;i++) cout<<sum[i].num<<" "<<sum[i].score<<endl;
return 0;
}
by Aybbl_xc @ 2023-07-27 11:23:59
#include <bits/stdc++.h>
using namespace std;
int n;
struct stu{
int chinese;
int math;
int english;
int score;
int num;
};
int main()
{
cin>>n;
stu a[301],sum[301];
for(int i=1;i<=n;i++){
cin>>a[i].chinese>>a[i].math>>a[i].english;
sum[i].score=a[i].chinese+a[i].math+a[i].english;
sum[i].num=i;
}
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
if(sum[i].score<sum[j].score){
swap(sum[i],sum[j]);
swap(a[i],a[j]);
}
else if(sum[i].score==sum[j].score){
if(a[i].chinese<a[j].chinese){
swap(sum[i],sum[j]);
swap(a[i],a[j]);
}
}
}
}
for(int i=1;i<=5;i++){
cout<<sum[i].num<<" "<<sum[i].score<<endl;
}
return 0;
}
当他遇到需要交换时,他要整体交换,把他的所有成绩和学号交换,不然下一次判断,就不是他的成绩
by Aybbl_xc @ 2023-07-27 11:24:09
@caimozeng044
by caimozeng044 @ 2023-07-27 11:27:34
谢谢