百度实习2017 编程题 Java
来源:互联网 发布:淘宝pc与手机尺寸 编辑:程序博客网 时间:2024/06/05 08:00
题目链接:
https://www.nowcoder.com/question/next?pid=4998655&qid=95827&tid=8085039
第三题用啥方法做会比较好…求解
动态规划求解
动态转移方程:ans[i][j] = ans[i-1][j-1](i-1-(j-1)) + ans[i-1][j](j+1)
解释说明:ans[i-1][j-1]*(i-j),i-1个数字的时候,只有j-1个小于号,那么添加数字的同时需要添加一个小于号,在i-1中就不能添加在最前面,所以总共有i-1位置可以添加,其中已经添加了j-1个,那么还剩下i-1-(j-1)=i-j个位置可以添加。
ans[i-1][j]*(j+1):如果i-1个数字中已经有j个小于号,那么新增加的数字不能添加小于号,所以插入的位置只能是已有的小于号两边,所以共有j+1种。
例如:(5,2)=(4,1)*(4-1)+(4,2)(2+1)
对于四个数字的排序的时候为:
4 2 3 1 –> 4>2< 3 > 1 插入数字5的时候,还需要插入一个小于号。 4 2 3 1排序中,能够插入的位置有 4后面、2 后面、4后面 1后面,但是 2后面已经被占用,所有剩下的位置为:4-1
2 3 4 1 – > 2 < 3 < 4 > 1 插入数字5的时候,不能添加小于号了。
当前排序中能够插入的位置 只能是在已经有小于号的数字附近,即第一个数的位置、2的后面、3的后面,所以有 2+1 个情况
package it.baidu;import java.util.Scanner;public class Main3 { static int n = 0; static int k = 0; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = sc.nextInt(); int[][] ans = new int[n+1][n+1]; for(int i=2;i<=n;i++){ ans[i][0] = 1; ans[i][i-1] = 1; } for(int i=3;i<=n;i++){ for(int j=1;j<i-1;j++){ ans[i][j] = (ans[i-1][j-1]*(i-j) + ans[i-1][j]*(j+1))%2017; } } System.out.println(ans[n][k]); sc.close(); }}
百度实习招聘——第二题
求三角形面积…
Java代码
package it.baidu;import java.util.ArrayList;import java.util.HashMap;import java.util.Scanner;public class Main2 { static double ans = 0.00000; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); HashMap<String, ArrayList<Integer[]>> map = new HashMap<>(); for (int i = 0; i < n; i++) { String str = sc.nextLine(); String[] strs = str.split(" "); ArrayList<Integer[]> list = new ArrayList<>(); Integer[] temp = new Integer[3]; temp[0] = Integer.valueOf(strs[1]); temp[1] = Integer.valueOf(strs[2]); temp[2] = Integer.valueOf(strs[3]); if (map.containsKey(strs[0])) { list = map.get(strs[0]); } list.add(temp); map.put(strs[0], list); } ArrayList<Integer[]> listR = map.get("R"); ArrayList<Integer[]> listG = map.get("G"); ArrayList<Integer[]> listB = map.get("B"); process1(listB); process1(listG); process1(listR); if (listB != null && listG != null && listR != null) { for (Integer[] b : listB) { for (Integer[] g : listG) { for (Integer[] r : listR) { process2(b, g, r); } } } } System.out.println(String.format("%.5f", ans)); sc.close(); } public static void process2(Integer[] b, Integer[] g, Integer[] r) { int x1 = b[0]; int y1 = b[1]; int z1 = b[2]; int x2 = g[0]; int y2 = g[1]; int z2 = g[2]; int x3 = r[0]; int y3 = r[1]; int z3 = r[2]; double len1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); double len2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3) + (z1 - z3) * (z1 - z3)); double len3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3) + (z2 - z3) * (z2 - z3)); if(len1==0||len2==0||len3==0) return; if(len1+len2>len3&&len1+len3>len2&&len3+len2>len1){ double p = (len1 + len2 + len3) / 2; double aera = Math.sqrt(p * (p - len1) * (p - len2) * (p - len3)); ans = Math.max(ans, aera); } } public static void process1(ArrayList<Integer[]> list) { if (list == null || list.size() < 3) { return; } for (int i = 0, size = list.size(); i < size; i++) { for (int j = i + 1; j < size; j++) { for (int k = j + 1; k < size; k++) { process2(list.get(i),list.get(j),list.get(k)); } } } }}
第一题:找出第三小的数
package it.baidu;import java.util.HashSet;import java.util.Scanner;public class Main1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; HashSet<Integer> set = new HashSet<>(); for(int i=0;i<n;i++){ nums[i] = sc.nextInt(); set.add(nums[i]); } if(set.size()<3){ System.out.println(-1); }else{ System.out.println(thirdMax(nums)); } sc.close(); } public static int thirdMax(int[] nums) { // 用long的主要作用是为了比较对应的Integer.MAX_VALUE 和 Integer.MIN_VALUE两个值 int[] temp = new int[3]; temp[0] = nums[0]; temp[1] = Integer.MAX_VALUE; temp[2] = Integer.MAX_VALUE; for (int i = 1; i < nums.length; i++) { if (nums[i] < temp[0]) { temp[2] = temp[1]; temp[1] = temp[0]; temp[0] = nums[i]; } else if (temp[0] < nums[i] && nums[i] < temp[1]) { temp[2] = temp[1]; temp[1] = nums[i]; } else if (temp[1] < nums[i] && nums[i] < temp[2]) { temp[2] = nums[i]; } } if (temp[2] == Integer.MAX_VALUE) return -1; return temp[2]; }}
0 0
- 百度实习2017 编程题 Java
- 百度2017实习编程题
- 百度2017暑期实习编程题
- 2017年京东实习编程题 ----Java
- 百度实习2017题目第3题
- 2017 Tencent实习编程题
- 2017阿里巴巴实习在线编程测验Java
- 百度Java开发实习生面经
- 百度java后台实习生面试题
- 百度实习
- 2017年腾讯暑期实习编程题
- 2017年华为实习编程题
- 2017网易实习 [编程题] 调整队形
- 实习编程题
- 网易实习--编程题
- [百度2016实习 在线笔试 编程第一题 度度熊 C语言]
- 阿里巴巴集团2017暑期实习Java研发工程师在线编程题-数组分片
- 2017/4/26阿里实习编程题:后缀表达式(Java实现)
- 图像特征提取系列之PCA
- 全排列问题,不等式数列
- addView
- [mysql] C++操作mysql方法总结
- 运算符重载,何时用友元,何时用成员函数
- 百度实习2017 编程题 Java
- 数据分析
- 【算法习题】棋盘上的距离
- Ramsis human-solutions
- 洛谷 1855——榨取kkksc03
- JVM读书笔记(一):Java内存区域与内存溢出异常
- wechall php系列之PHP 0817
- 汇编学习笔记
- dAO模式理解一