百度实习2017 编程题 Java

题目链接:
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值