時間限制 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;
}