分数 20
全屏浏览题目
切换布局
作者 李廷元单位 中国民用航空飞行学院
本题目要求读入n个整数,要求用最少的比较次数,输出它们的最小值和次小值。例如,对于12 13 1 10 34 1这6个数,最小值为1,次小值
为10。
输入格式:
输入有两行。第一行为整数个数n(≤1 000 000),第二行给出n个以空格分隔的整数。
输出格式:
对每一组输入,在一行中输出最小值和次小值,中间以一个空格分隔,但行尾没有多余空格。如果输入数据不足两个,则输出“Invalid Input”。如果没有次小值,则输出“There is no second smallest element”。
输入样例:
6
12 13 1 10 34 1
输出样例:
1 10
code:
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
if(n<2) cout<<"Invalid Input"<<endl;
else if(a[0]==a[n-1]) cout<<"There is no second smallest element"<<endl;
else {
int t,k;
t=a[0];cout<<t<<" ";
for(int i=1;i<n;i++){
if(a[i]!=t) {
cout<<a[i]<<endl;
return 0;
}
}
}
return 0;
}