王小博123 @ 2021-02-26 19:16:33
#include<stdio.h>
struct xushen
{
int a;
int b;
int c;
int d;
int e;
};
int main()
{
int n,i,j;
struct xushen k[305],max;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
k[i].a=i+1;
scanf("%d%d%d",&k[i].b,&k[i].c,&k[i].d);
getchar();
k[i].e=k[i].b+k[i].c+k[i].d;
}
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(k[j].e<k[j+1].e||k[j].e==k[j+1].e&&k[j].b<k[j+1].b||k[j].e==k[j+1].e&&k[j].b==k[j+1].b&&k[j].a>k[j+1].a)
{
max.a=k[j+1].a;
max.e=k[j+1].e;
k[j+1].a=k[j].a;
k[j+1].e=k[j].e;
k[j].a=max.a;
k[j].e=max.e;
}
for(i=0;i<5;i++)
printf("%d %d\n",k[i].a,k[i].e);
return 0;
## 为什么当总分相同时,语文成绩高的就不会和低的交换,我那里已经写了if语句啊啊啊
by Textbook_blasphemy @ 2021-02-26 19:20:04
都写出来结构体了为什么不用sort
by Textbook_blasphemy @ 2021-02-26 19:22:54
@王小博123 运算符优先级问题,建议加括号或用cmp函数+sort
by 王小博123 @ 2021-02-26 19:36:22
@陶(戴)佳伟 我加了括号也还是不行,啊,是哪出了问题呀
by angel0617 @ 2021-08-13 16:46:37
#include <iostream>
#include <algorithm>
using namespace std;
struct Person
{
int id, chi, math, eng, sum;
void read()
{
cin >> chi >> math >> eng;
}
};
Person a[310];
bool cmp(Person x,Person y)
{
if(x.sum != y.sum)return x.sum > y.sum;
if(x.chi != y.chi)return x.chi > y.chi;
return x.id < y.id;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
a[i].read();
a[i].sum = a[i].chi + a[i].math + a[i].eng;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= 5; i++)
{
cout << a[i].id << " " << a[i].sum <<endl;
}
return 0;
}