首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)
如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1
10 10 10 10 10 20 20 30 30 40 40
30
package baidu;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0;
try{
System.out.println("请输入生成帽子的个数:");
count = scan.nextInt();
}
catch (Exception e){
e.printStackTrace();
System.out.println("非法,只能输入数字");
}
finally {
scan.close();
}
long start = System.currentTimeMillis();
int[] priceArray = new int[count];
Random rand = new Random();
for(int i=0;i<count;i++){
priceArray[i] = rand.nextInt(1000);
}
// 测试用例数据--最变态的情况
//priceArray = new int[]{30,30,30, 30 ,30, 30, 30, 30, 40, 40};
// 测试用例数据--比较变态的情况
// priceArray = new int[]{10,10, 10, 10 ,20, 20, 30, 30, 40, 40};
System.out.print(count+"个随机的价格是:");
for(int i=0;i<priceArray.length;i++){
System.out.print(priceArray[i]);
System.out.print(",");
}
System.out.println("");
// 对所有的价格进行了升序排序
Arrays.sort(priceArray);
System.out.print(count+"排完序的价格是:");
for(int i=0;i<priceArray.length;i++){
System.out.print(priceArray[i]);
System.out.print(",");
}
System.out.println("");
// 然后找出最贵和第二贵的是不是有重复的数值,则第三贵的帽子应该是从数组里往下串 start
// 最贵的帽子有几个,初始是1
int firstCount = 1;
for(int i=priceArray.length-1;i>0;i--){
if(priceArray[i] == priceArray[i-1] ){
firstCount++;
}
else{
break;
}
}
// 第二贵的帽子位置,初始是第一贵的后面一个
int secondHighIndex = priceArray.length-firstCount-1;
// 第二贵的的帽子有几个,初始是1
int secondCount = 1;
for(int i=secondHighIndex;i>0;i--){
if(priceArray[i] == priceArray[i-1] ){
secondCount++;
}
else{
break;
}
}
// 然后找出最贵和第二贵的是不是有重复的数值,则第三贵的帽子应该是从数组里往下串 end
// 第三贵的的帽子的位置
int thirdHighIndex = priceArray.length-(firstCount+secondCount)-1;
long end = System.currentTimeMillis();
double during = (double) (end-start)/1000 ;
if(thirdHighIndex == -1){
System.out.println("价格第三高的帽子是:"+thirdHighIndex);
}
else{
System.out.println("价格第三高的帽子是:"+priceArray[thirdHighIndex]);
}
System.out.println("运行了"+ during +"秒的时间");
}
在编译器能实现,但是提交时候报数组越界🙄import java.util.*;
public class ByHat{ public static void main(String[] args) { // 首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000) Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int num = sc.nextInt(); System.out.println(num); Scanner sct = new Scanner(System.in); while (num > 0) { int data = sct.nextInt(); if(!list.contains(data)){ list.add(data); } num--; } Collections.sort(list); if (list == null || list.size() < 3) System.out.println(-1); else System.out.println(list.get(2)); sc.close(); sct.close(); } }
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 百度2017面试题买帽子
*
* @author 恒哥
*
* @date 2018年1月10日
*/
public class Main { static int N = 0; // 设置数组的大小N,初始为0,后面根据控制台输入的值修改 static int[] arr; // 创建int类型的数组,保存从控制台输入的数据 static int index = 0; // 编号,标注第三便宜的帽子的位置 public static void main(String args[]) throws IOException { // 从控制台输入的方法,因为涉及到空格,无法用Scanner处理,所以用了BufferReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 读取控制台输入的帽子的数量 N = Integer.parseInt(br.readLine()); // 将数量N设置为int数组的大小 arr = new int[N]; // 以字符串的格式从控制台读取输入的数组 String str = br.readLine(); // 因为中间用空格隔开,所以将其转化为字符串数组 String[] strArr = str.split(" "); // 将字符串数组的内容复制到int数组中,如果字符串数组中有数字不符合要求(>1000或<0),则将对应的int数组的位置的值设为0 for (int i = 0; i < strArr.length; i++) { arr[i] = Integer.parseInt(strArr[i]); } // 冒泡排序 for (int i = 0; i < N; i++) { for (int j = 0; j < N - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // 从最小的数开始遍历int数组,如果连续几个数都相等,则index的值不变,如果数组的值变大,则index++ int temp = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] > 0) { temp = arr[i]; break; } } for (int i = 0; i < arr.length; i++) { if (arr[i] > 0 && arr[i] < 1000 && arr[i] > temp) { index++; temp = arr[i]; } // 当index自增到2时,说明已经遍历到了第3大的价格 if (index == 2) { System.out.println(arr[i]); break; } } if (index < 2) { System.out.println(-1); } }
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int times = 0;
Arrays.sort(arr);
for (int j = 1; j < arr.length; j++) {
if (arr[j] != arr[j - 1]) {
times++;
} else {
continue;
}
if (times == 2) {
System.out.print(arr[j]);
return;
}
}
System.out.print(-1);
}
}