C++题目求解
#include<iostream>
#include<string.h>
using namespace std;
struct st{//定义一个结构体
char name[11];//依次姓名,4科成绩;
int a;
int b;
int c;
int d;
};
int main()
{
struct st s[50];//定义一个结构体数组;
int n,z[111],k=0,t;
char h[11];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i].name>>s[i].a>>s[i].b>>s[i].c>>s[i].d;
z[k++]=s[i].a+s[i].b+s[i].c+s[i].d;//4科成绩的和赋给数组z
}
for(int i=0;i<k;i++)
{
for(int j=0;j<k-i-1;j++)
{
if(z[j]<z[j+1])
{
t=z[j];//总成绩交换,下面名字也交换;
z[j]=z[j+1];
z[j+1]=t;
strcpy(h,s[j].name);
strcpy(s[j].name,s[j+1].name);
strcpy(s[j+1].name,h);
}
}
}
for(int i=0;i<3;i++)//输出前三个名字;
{
cout<<s[i].name<<endl;
}
return 0;
}
希望能帮到你,望采纳;
2020-03-22
#include <iostream>
#include <algorithm>
using namespace std;
struct transcript {
char name[11];
int Math, Chinese, English, Geography, sum;
} s[40];
bool cmp(transcript a, transcript b) {
return a.sum > b.sum;
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> s[i].name >> s[i].Math >> s[i].Chinese >> s[i].English >> s[i].Geography;
s[i].sum = s[i].Math + s[i].Chinese + s[i].English + s[i].Geography;
}
sort(s, s + N, cmp);
for (int i = 0; i < 3; i++)
cout << s[i].name << endl;
return 0;
}