BUPT復試專題—最值問題(2013計院)
阿新 • • 發佈:2018-03-28
blog mat 中間 frame sub 輸入格式 一個 sca esc
題目描述
給出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<cmath> #include<cstring> #include<string> #include<map> using namespace std; int main() { int tes,m; while(~scanf("%d",&tes)) { while(tes--) { cin>>m; map<int,int>donser;while(m--) { int a; cin>>a; donser[a]=1; } map<int,int>::iterator it; int x=2; for(it=donser.end();it!=donser.begin()&&x;) { it--; x--; if(x==1) cout<<it->first<<" "; else cout<<it->first<<endl; } } } return 0; }
BUPT復試專題—最值問題(2013計院)