2018界 蓝桥杯Java B组代码题解

导言

第一次写博客,发题解,如果有哪些不符合规范的,敬请提出.
以下代码均自己所写,基本都运用暴搜的思想,一定有不完善的地方,恳求各位大佬指出,共同学习.
代码注释比较少,如果不理解的地方或者错误的地方,请在评论区指出,我定积极探讨.

第六题:递增三元组

   给定三个整数数组
    A = [A1, A2, ... AN],
    B = [B1, B2, ... BN],
    C = [C1, C2, ... CN],
    请你统计有多少个三元组(i, j, k) 满足:

    1. 1 <= i, j, k <= N
    2. Ai < Bj < Ck
	
	【输入格式】
    第一行包含一个整数N。
    第二行包含N个整数A1, A2, ... AN。
    第三行包含N个整数B1, B2, ... BN。
    第四行包含N个整数C1, C2, ... CN。

    对于30%的数据,1 <= N <= 100
    对于60%的数据,1 <= N <= 1000
    对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

    【输出格式】
    一个整数表示答案

    【输入样例】
    3
    1 1 1
    2 2 2
    3 3 3

    【输出样例】
    27


    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
import java.util.Scanner;
class test {
    private static int sum;
    //递归查询:n是记录已经挑选过的个数
    public static void helper(int A[], int B[], int C[], int curr[], int n, int N) {
        //递归终点,判断
        if (n == 3) {
            if (A[curr[0]] < B[curr[1]] && B[curr[1]] < C[curr[2]]) {
                sum++;
            }
        } else {
            //遍历挑选0-N的数,记录在curr数组中
            for (int i = 0; i < N; i++) {
//                int temp=curr[n];
                curr[n] = i;
                helper(A, B, C, curr, n + 1, N);
//                curr[n]=temp;//回溯这一步可有可无
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] A = new int[N];
        int[] B = new int[N];
        int[] C = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt();
        }
        for (int i = 0; i < N; i++) {
            B[i] = sc.nextInt();
        }
        for (int i = 0; i < N; i++) {
            C[i] = sc.nextInt();
        }
        //以上均为例题输入
        int[] curr = new int[3];
        helper(A, B, C, curr, 0, N);
        System.out.println(sum);
    }
}

测试数据:
5
1 2 1 3 4
2 2 2 3 2
3 5 1 3 5
输出:38

在这里插入图片描述

第七题:螺旋折线

    如图p1.pgn所示的螺旋折线经过平面上所有整点恰好一次。
    对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。

    例如dis(0, 1)=3, dis(-2, -1)=9

    给出整点坐标(X, Y),你能计算出dis(X, Y)吗?

    【输入格式】
    X和Y

    对于40%的数据,-1000 <= X, Y <= 1000
    对于70%的数据,-100000 <= X, Y <= 100000
    对于100%的数据, -1000000000 <= X, Y <= 1000000000

    【输出格式】
    输出dis(X, Y)


    【输入样例】
    0 1

    【输出样例】
    3
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms


    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    不要使用package语句。不要使用jdk1.7及以上版本的特性。
    主类的名字必须是:Main,否则按无效代码处理。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class test{
    public static void main(String[] args) {
        //0,1,2,3代表北东南西,0代表横坐标,1代表纵坐标
        //dir[我面朝哪个方向][横纵坐标]=横纵坐标的变化
        int[][] dir = new int[4][2];
        {   dir[0][0]=1;
            dir[0][1]=0;
            dir[1][0]=0;
            dir[1][1]=-1;
            dir[2][0]=-1;
            dir[2][1]=0;
            dir[3][0]=0;
            dir[3][1]=1;}
        //go表示继续直走
        int[][] go=new int[4][2];
        {   go[0][0]=0;
            go[0][1]=1;
            go[1][0]=1;
            go[1][1]=0;
            go[2][0]=0;
            go[2][1]=-1;
            go[3][0]=-1;
            go[3][1]=0;}
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int m=-1;
        int n=0;//(m,n)记录当前点
        int key=3;//记录方向
        int dit=1;//记录距离
        //以上均为例题输入和初始化
        //Map记录该坐标有没有被走过,这里用String来表示坐标
        Map<String, Boolean> map=new HashMap<>();
        //初始化两个点,这里就假设距离大于2了,但比赛时要添加上相应输出
        map.put("00",true);
        map.put("-10",true);
        //用(m,n)来记录位置
        while (m!=x||n!=y){
            //把整数变成字符串,用来查询表:true代表走过,false代表没走过
            String c= (m+dir[key][0])+String.valueOf(n+dir[key][1]);
            if(map.containsKey(c)){//右手边走过,那么继续保持该方向
                m=go[key][0]+m;
                n=go[key][1]+n;
                //map记录该点被走过
                String d=(m)+String.valueOf(n);
                map.put(d,true);
                dit++;
            }else {//右手边没走过,向右转
                m=m+dir[key][0];
                n=n+dir[key][1];
                key=key==3?0:key+1;
                //更新map
                map.put(c,true);
                dit++;
            }
        }
        System.out.println(dit);
    }
}

测试数据: 100 -100
输出: 40200

在这里插入图片描述

第八题:日志统计

    小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:
    ts id

    表示在ts时刻编号id的帖子收到一个"赞"。

    现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。

    具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。

    给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。

    【输入格式】
    第一行包含三个整数N、D和K。
    以下N行每行一条日志,包含两个整数ts和id。

    对于50%的数据,1 <= K <= N <= 1000
    对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000

    【输出格式】
    按从小到大的顺序输出热帖id。每个id一行。

    【输入样例】
   N  D  K
    7 10 2
   ts id
    0 1
    0 10
    10 10
    10 1
    9 1
    100 3
    100 3

    【输出样例】
    1
    3

    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//自定义一个类:包含了每条日志的ts与id的对应关系
class ts_id {
    int ts;
    int id;
    public ts_id(int ts, int id) { this.ts = ts;this.id = id; }
}
public class test {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();//N条
        int D=sc.nextInt();//时间段
        int K=sc.nextInt();//热帖数量
        ts_id[] nums=new ts_id[N];
        for(int i=0;i<N;i++){
            nums[i]= new ts_id(sc.nextInt(),sc.nextInt());
            //若要测试10000的数则替换为:nums[i]= new ts_id((int)(Math.random()*200),((int)(Math.random()*100)));
        }//以上为例题输入
        //两个哈希表分别记录:①每条日志id和时间的关系②该id和对应的有效赞次数
        Map<Integer,Integer> id_ts=new HashMap<>();
        Map<Integer,Integer> id_time=new HashMap<>();
        for (ts_id i : nums) {
            //如果id_ts有
            if (id_ts.containsKey(i.id)){
                //如果在时间内
                if(id_ts.get(i.id)+D>i.ts){
                    id_time.put(i.id,id_time.get(i.id)+1);
                    //如果满足K,打印输出该id
                    if(id_time.get(i.id)>=K) System.out.println(i.id);
                }else {
                    //如果不在时间内,更新id_ts
                    id_ts.put(i.id,i.ts);
                }
            }else {//如果id_ts没有此记录
                id_ts.put(i.id,i.ts);
                id_time.put(i.id,1);
            }
        }
    }
}

测试数据:
7 10 2
0 1
0 10
10 10
10 1
9 1
10 3
20 10
输出:
1
在这里插入图片描述

第九题:全球变暖

    你有一张某海域NxN像素的照片,"."表示海洋、"#"表示陆地,如下所示:

    .......
    .##....
    .##....
    ....##.
    ..####.
    ...###.
    .......

    其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。

    由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。

    例如上图中的海域未来会变成如下样子:

    .......
    .......
    .......
    .......
    ....#..
    .......
    .......

    请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

    【输入格式】
    第一行包含一个整数N。  (1 <= N <= 1000)
    以下N行N列代表一张海域照片。

    照片保证第1行、第1列、第N行、第N列的像素都是海洋。

    【输出格式】
    一个整数表示答案。

    【输入样例】
    7
    .......
    .##....
    .##....
    ....##.
    ..####.
    ...###.
    .......

    【输出样例】
    1



    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
import java.util.Scanner;

class test {
    static int daoyu;
    private static int sum;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        char[][] graph = new char[N][N];
        for (int i = 0; i < N; i++) {
            String curr = sc.next();
            for (int j = 0; j < N; j++) {
                graph[i][j] = curr.charAt(j);
            }
        }//以上为例题输入
        //接着确定岛屿数量
        for (int i = 1; i < N - 2; i++) {
            for (int j = 1; j < N - 2; j++) {
                if (graph[i][j] == '#') {
                    graph[i][j] = '1';//把每个遍历过的岛屿都变成'1'来代替
                    check(graph, i, j, N);//查询连通块
                    daoyu++;
                }
            }
        }
        //查询被淹没后剩余岛屿的数量
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (graph[i][j] == '1' && check2(graph, i, j)) {
                    sum++;
                }
            }
        }
        System.out.println(daoyu - sum);
    }
    //查询DFS连通块
    public static void check(char[][] graph, int i, int j, int N) {
        if (i != N && graph[i + 1][j] == '#') {
            graph[i + 1][j] = '1';
            check(graph, i + 1, j, N);
        }
        if (j != N && graph[i][j + 1] == '#') {
            graph[i][j + 1] = '1';
            check(graph, i, j + 1, N);
        }
        if (i != 0 && graph[i - 1][j] == '#') {
            graph[i - 1][j] = '1';
            check(graph, i - 1, j, N);
        }
        if (j != 0 && graph[i][j - 1] == '#') {
            graph[i][j - 1] = '1';
            check(graph, i, j - 1, N);
        }
    }
    //判断周围有没有海
    public static boolean check2(char[][] graph, int i, int j) {
        if (graph[i - 1][j] == '1' && graph[i][j - 1] == '1' && graph[i + 1][j] == '1' && graph[i][j + 1] == '1')
            return true;
        return false;
    }
}

测试数据:
9

…##…##.
.###…##.
.#####…
.#…##.
.#…###…
.#…####.
…#…

打印结果:1
在这里插入图片描述

第十题:堆的计数(不好意思没做出来,附上第一次思想代码,留给我自己看)

    我们知道包含N个元素的堆可以看成是一棵包含N个节点的完全二叉树。
    每个节点有一个权值。对于小根堆来说,父节点的权值一定小于其子节点的权值。

    假设N个节点的权值分别是1~N,你能求出一共有多少种不同的小根堆吗?

    例如对于N=4有如下3种:

    1
    / \
    2   3
    /
    4

    1
    / \
    3   2
    /
    4

    1
    / \
    2   4
    /
    3

    由于数量可能超过整型范围,你只需要输出结果除以1000000009的余数。


    【输入格式】
    一个整数N。
    对于40%的数据,1 <= N <= 1000
    对于70%的数据,1 <= N <= 10000
    对于100%的数据,1 <= N <= 100000

    【输出格式】
    一个整数表示答案。

    【输入样例】
    4

    【输出样例】
    3


    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
class treeNode{
    int val;
    treeNode left;
    treeNode right;
    public treeNode(int x){
        this.val =x;
    }
}
class _2018 {
    static int sum;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int[] nums=new int[sc.nextInt()];
        for (int i=0;i<nums.length;i++){
            nums[i]=i;
        }
        helper(nums.length,0,nums);
        System.out.println(sum);
    }
    public static void helper(int n, int curr,int[] nums){
        if(curr==n){
            if(nums[0]!=1) return;
            treeNode root=new treeNode(1);
            if (check(nums,1,root)) sum++;
        }else {
            for(int i=curr;i<n;i++){
                {int temp=nums[curr];
                    nums[curr]=nums[i];
                    nums[i]=temp;}
                helper(n, curr + 1, nums);
                int temp=nums[curr];
                nums[curr]=nums[i];
                nums[i]=temp;
            }
        }
    }
    //检查是否符合树的条件
    private static boolean check(int[] nums,int i,treeNode root) {
        if(i+1==nums.length-1) return nums[i+1]>nums[i];
        if(nums[i+1]>nums[i]){
            root.left=new treeNode(nums[i+1]);
            boolean c=check(nums,i+1,root.left);
            root.right=new treeNode(nums[i+1]);
            boolean d=check(nums,i+1,root.left);
            if(c&&d) sum=sum+1;
            return c||d;
        }
        return false;
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值