8.27 京东笔试 2ac

1.使用队列
package jingdong_01;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Deque<Integer> q = new LinkedList<>();
        q.addLast(2);
        q.addLast(3);
        q.addLast(5);
        int k = 0;
        while (!q.isEmpty()){
            int temp = q.pollFirst();
            k++;
            if(k == n){
                System.out.println(temp);
                break;
            }
            q.addLast(temp * 10 + 2);
            q.addLast(temp * 10 + 3);
            q.addLast(temp * 10 + 5);
        }
    }
}
2.dp动态规划,处理输入时一开始用他提示的方式,咋都出错,最后还是用了nextInt(),ac;
package jingdong_02;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[][] = new int[n][2*n - 1];
        for(int i = 0;i < n;i++){
            for(int k = (2*n-1)/2 - i;k < (2*n - 1)-((2*n-1)/2 - i);k++){
                a[i][k] = sc.nextInt();
            }
        }
        for(int i = 1;i < n;i++){
            for(int j = 0;j < a[0].length;j++){
                if(j == 0){
                    a[i][j] = Math.max(a[i - 1][j],a[i - 1][j + 1]) + a[i][j];
                }else if(j == a[0].length - 1){
                    a[i][j] = Math.max(a[i - 1][j],a[i - 1][j - 1]) + a[i][j];
                }else{
                    int temp = Math.max(a[i - 1][j],a[i - 1][j - 1]);
                    a[i][j] = Math.max(temp,a[i - 1][j + 1]) + a[i][j];
                }
            }
        }
        int res = a[n - 1][0];
        for(int k = 0;k < a[0].length;k++){
            if (a[n - 1][k] > res){
                res = a[n - 1][k];
            }
        }
        System.out.println(res);
    }
}



#笔试题目##京东#
全部评论
第二题有魔法排打boss的吗?
1
送花
回复
分享
发布于 2020-08-27 21:23
第二题大概是啥题目啊
点赞
送花
回复
分享
发布于 2020-08-28 00:13
滴滴
校招火热招聘中
官网直投
请问大佬有滚动小球那题的c++代码吗 输入搞不清楚
点赞
送花
回复
分享
发布于 2020-08-28 00:26

相关推荐

点赞 评论 收藏
转发
1 1 评论
分享
牛客网
牛客企业服务