多校联考
【试题描述】
现在有N个学校在进行多校联考,每个学校有若干个学校在参加联考,教练们非常重视尖子生培养,他们只关心每个学校前三名的成绩,现在给出N个学校联考的成绩,想让大佬您帮忙找出哪个学校前三名总分和最高,如果有相同的,输出序号靠前的:
【输入格式】
第一行输入一个数N,表示有N个学校;
接下来有N行,每行若干个用空格隔开的正整数,表示学生成绩,每行以0结尾:
【输出格式】
输出包含一行,共两个数,分别表示前三名总分最高的分数和及学校序号:
【样例输入1】
3
280 280 220 0
280 120 260 0
300 300 180 0
【样例输出1】
780 1
【样例输入2】
3
250 190 300 200 180 0
300 120 260 180 280 0
300 300 180 100 0
【样例输出2】
840 2
【数据范围】
对于20%的数:1<N<=3,每行不包括行末的0为3个数,如:3003002000:
对于100%的数:1<N<=5,每行不超过500个数,每个数小于等于300:
这道题没必要排序
#include <iostream>
using namespace std;
int top3_total()
{
int score, first = 0, second = 0, third = 0;
while (cin >> score)
{
if (score == 0)
break;
if (score > first)
{
third = second;
second = first;
first = score;
}
else if (score > second)
{
third = second;
second = score;
}
else if (score > third)
{
third = score;
}
}
return first + second + third;
}
int main()
{
int n;
cin >> n;
int max = 0, j;
for (int i = 1; i <= n; i++)
{
int top = top3_total();
if (top > max)
{
max = top;
j = i;
}
}
cout << max << ' ' << j;
return 0;
}
#include<iostream>
using namespace std;
void sort(int t[],int n)
{
for(int j=0; j<n-1; j++)
for(int h=0; h<n-1-j; h++)
if(t[h]<t[h+1])
{
int tem=t[h];
t[h]=t[h+1];
t[h+1]=tem;
}
}
int main()
{
int n,t[600],sum[6]= {0},h=0,j;
cin >> n;
for(j=0; j<n; j++)
{
h=0;
while(cin >>t[h],t[h]!=0)
{
h++;
}
sort(t,h);
sum[j]=t[0]+t[1]+t[2];
}
int max=sum[0],pos;
for(j=1; j<n; j++)
{
if(sum[j]>max)
{
max=sum[j];
pos=j;
}
}
if(max==sum[0])
pos=0;
cout << max <<" "<<pos+1<< endl;
return 1;
}