百度2017年春招编程笔试题
来源:互联网 发布:caffe windows 编译 编辑:程序博客网 时间:2024/06/05 09:20
[编程题] 买帽子时间限制:1秒空间限制:32768K度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少? 输入描述:首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)输出描述:如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1输入例子:1010 10 10 10 20 20 30 30 40 40输出例子:30
解题:
import java.util.Scanner;import java.util.*;public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int count=0; int i; int j=0; int[] array = new int[N]; for(i=0;i<N;i++) { array[i]=scan.nextInt(); } for (i = 0; i < array.length; i++) { for (j = i; j < array.length; j++) { if (array[i] > array[j]) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } } if(N<=2) { System.out.println(-1); return; } for(i=0;i<N-1;i++) { if(array[i]<array[i+1]) { count++; if(count ==2 ) { System.out.println(array[i+1]); } } } if(count<2) { System.out.println(-1); } }}
阅读全文
1 0
- 百度2017年春招编程笔试题
- 【笔试】百度编程题
- 2017百度实习生笔试编程题
- 百度2017秋招笔试编程题
- 一道2017年百度笔试编程题
- 百度2017秋招笔试编程题
- 百度笔试 编程题集锦
- 百度2017春招笔试真题编程题集合
- 2017百度实习生招聘笔试真题编程题集合
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试 [编程题] 买帽子
- 百度2017春招笔试的部分编程题
- 百度的两道笔试编程题
- php 数据处理--合并,拆分,追加,去重, 多数据
- C# 简单操作XML文档(增删查改)
- php的垃圾回机制
- window.parent & window.opener
- 区间合并算法
- 百度2017年春招编程笔试题
- Mysql(二) 聚合函数 group by , having, inner join , limit
- vue.js入门笔记
- 正则表达式获取url参数
- IDA打开自动显示注释选项
- 通过类加载器解决jar包冲突(一)
- 浏览器事件对象兼容写法
- postgresql编译用包分析整理
- h5新标签