时间限制 1000 ms 内存限制 65536 KB
题目描述
给出N个数,求出这N个数中最大值和次大值。注意这里的次大值必须严格小于最大值。输入保证N个数中至少存在两个不同的数。
输入格式
第一行为测试数据的组数T(T≤20)。请注意,任意两组测试数据之间是相互独立的。
每组数据包括两行:
第一行为一个整数N(1≤N≤1000)。
第二行为N个正整数,每个整数均不大于106。
输出格式
每组数据输出两个数,即N个数的最大值和次大值,中间用空格隔开。
输入样例
2
5
1 2 3 4 5
4
2 2 2 3
输出样例
5 4
3 2
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXSIZE 1010
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int t,n;
int max;
int buf[MAXSIZE];
scanf("%d",&t);
while (t--){
//input
scanf("%d",&n);
for (int i=0;i<n;i++){
scanf("%d",&buf[i]);
}
//sort
sort(buf,buf+n,cmp);
//output
max=buf[0];
for (int i=1;i<n;i++){
if (buf[i]<max){
printf("%d %d\n",max,buf[i]);
break;
}
}
}
return true;
}