买帽子
来源:互联网 发布:易语言钓鱼软件源码 编辑:程序博客网 时间:2024/04/28 09:00
时间限制:1秒
空间限制:32768K
输入描述:
首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)
输出描述:
如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1
输入例子:
1010 10 10 10 20 20 30 30 40 40
输出例子:
30
import java.util.Scanner;
public class SuanFa {
public static void main(String[] args)
{
int[] price = new int[55];
int[] NewPrice = new int[1005];
Scanner scaner = new Scanner(System.in);
int count = scaner.nextInt();
int k = 0;
for(int j= 0;j< count;j++)
NewPrice[j] = 0;
for(int i=0;i<count;i++)
{
int p = scaner.nextInt();
NewPrice[p]++;
if(NewPrice[p] == 1)
price[k++] = p;
}
int thirdP = getThirdPrice(price,0,k-1,k);
if(thirdP == -1)
System.out.println(-1);
else
System.out.println(thirdP);
}
static int getThirdPrice(int[] price,int low,int high,int count)
{
if(count < 3)
return -1;
int index = getIndex(price, low, high);
while(index != 2)
{
if(index == 2)
break;
else if(index > 2)
index = getIndex(price,low,index-1);
else
index = getIndex(price,index+1,high);
}
return price[2];
}
static int getIndex(int[] price,int low,int high)
{
int pivot = price[low];
while(low < high)
{
while(low < high && price[high] >= pivot)
high--;
price[low] = price[high];
while(low < high && price[low] <= pivot)
low++;
price[high] = price[low];
}
price[low] = pivot;
return low;}
}
- 买帽子
- 买帽子
- 买帽子
- 2980 买帽子
- 【codevs2980】买帽子
- code vs 买帽子
- Codevs 2980 买帽子
- codevs2980买帽子
- CDOEVS 2980 买帽子
- 百度笔试- 买帽子
- 牛客网 买帽子
- [编程题] 买帽子
- [编程题] 买帽子
- 【算法题】买帽子
- 校招真题 百度 买帽子
- [编程题] 买帽子
- 买帽子 (hash)
- [编程题] 买帽子
- Installing software for ubuntu 16.04.2 LTS
- JSP —— one
- C# 枚举
- IOS
- linux64系统运行32位程序
- 买帽子
- 在使用Thinkphp的自我验证时出现非法数据对象的错误
- JSP —— two
- 计算广告小结[上]
- APPIUM Android 定位方式
- Android初级教程
- Python 2.7.8 手动安装 & Python 环境变量添加
- (转)芒格:投资成功不是频繁交易,而是好的决策和耐心
- Ubuntu开启core dump