輸入描述:
首先輸入一個正整數N(N <= 50),接下來輸入N個數表示每頂帽子的價格(價格均是正整數,且小於等於1000)
輸出描述:
如果存在第三便宜的帽子,請輸出這個價格是多少,否則輸出-1
輸入例子1:
10 10 10 10 10 20 20 30 30 40 40
輸出例子1:
30
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int pr[50];
int i,j,k;
for(i=0;i<n;i++){
cin>>pr[i];
}
// n=5;i=5;
int temp[3] = {1001,1001,1001};
// cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<endl;
for(i=0;i<n;i++){
while(pr[i]==temp[0]||pr[i]==temp[1]||pr[i]==temp[2])
i++;
for(k=0;k<3;k++){
if(temp[k] > pr[i]){
temp[k] = pr[i];
// cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<endl;
break;
}
// cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<endl;
}
}
int max=temp[0];
for(j=1;j<3;j++){
if(temp[j] > max)
max = temp[j];
}
if(temp[0]==1001||temp[1]==1001||temp[2]==1001)
max=-1;
cout<<max;
}