fujiayu20050525 @ 2021-04-18 09:50:56
#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int number;
int minTotal=1000;
int minPosition=-1;
struct student{
int ID;
int total;
int chinese;
int math;
int english;
}School[301];
int main(){
cin>>number;
for (int i=1;i<=number;i++){
School[i].ID=i;
cin>>School[i].chinese>>School[i].math>>School[i].english;
School[i].total=School[i].chinese+School[i].math+School[i].english;}
for(int i=1;i<=number;i++){
int pos=i;
for(int j=i+1;j<=number;j++){
if(School[j].total<School[pos].total){
pos = j;
}
}
swap(School[pos],School[i]);
}
for (int i=2;i<=number;i++){
if (School[i-1].total==School[i].total){
if(School[i-1].chinese>School[i].chinese){
swap(School[i-1],School[i]);
}
else if(School[i-1].ID<School[i].ID){
swap(School[i-1],School[i]);
}
}
}
for (int i=number;i>=number-4;i--){
cout<<School[i].ID<<" "<<School[i].total<<endl;
}
return 0;
}
各位大佬,为什么数据点七和数据点八过不了啊?
by fujiayu20050525 @ 2021-04-18 09:51:50
for (int i=2;i<=number;i++){
if (School[i-1].total==School[i].total){
if(School[i-1].chinese>School[i].chinese){
swap(School[i-1],School[i]);
}
else if(School[i-1].ID<School[i].ID){
swap(School[i-1],School[i]);
}
}
}
把这一段复制一遍再测试7,8都过了,但是六又错了
by PIKA_PIKA @ 2021-04-18 09:53:41
@fujiayu20050525
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,i,j;
cin>>n;
int a1,b1,c1,s[n],c[n],d[n];
for(i=0;i<n;i++){
cin>>a1>>b1>>c1;
d[i]=a1;
c[i]=a1+b1+c1;
s[i]=i+1;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(c[j]>c[i] || (c[j]==c[i] && d[i]<d[j])){
swap(c[j],c[i]);
swap(s[j],s[i]);
swap(d[j],d[i]);
}
}
}
for(i=0;i<5;i++){
cout<<s[i]<<" "<<c[i]<<endl;
}
}
by XiaoQuQu @ 2021-04-18 11:51:05
贴一下我的代码 你自己参考吧。
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct STUDENT {
int yw;
int tot;
int xh;
} a[305];
bool cmp(struct STUDENT a, struct STUDENT b) {
if (a.tot == b.tot) {
if (a.yw == b.yw) {
return a.xh < b.xh;
}
return a.yw > b.yw;
}
else {
return a.tot > b.tot;
}
}
int main(void) {
int n, y, s, x;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &y, &s, &x);
a[i].yw = y; a[i].tot = y + s + x;
a[i].xh = i;
}
sort(a, a+n, cmp);
for (int i = 0; i < 5; ++i) {
printf("%d %d\n", a[i].xh + 1, a[i].tot);
}
return 0;
}