2021年蓝桥杯javaB组

A
已知大写字母 A 的 ASCII 码为 65,请问大写字母 L 的 ASCII 码是多少?

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {

        System.out.println(65+'L'-'A');
    }
}

B
小蓝有很多数字卡片,每张卡片上都是数字 00 到 99。 小蓝准备用这些卡片来拼一些数,他想从 11 开始拼出正整数,每拼一个, 就保存起来,卡片就不能用来拼其它数了。 小蓝想知道自己能从 11 拼到多少。 例如,当小蓝有 3030 张卡片,其中 00 到 99 各 33 张,则小蓝可以拼出 11 到 1010, 但是拼 1111 时卡片 11 已经只有一张了,不够拼出 1111。 现在小蓝手里有 00 到 99 的卡片各 20212021 张,共 2021020210 张,请问小蓝可以从 11 拼到多少? 提示:建议使用计算机编程解决问题
思路:暴力解法
弄个count记录写到了多少,弄1个数组记录卡片还有多少张

import java.util.Arrays;
import java.util.Scanner;


// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        int[] counts = new int[10];
        for (int i = 0; i < counts.length; i++) {
            counts[i] = 2021;
        }
        int i = 0;
        while (counts[0] > 0 && counts[1] > 0 && counts[2] > 0 && counts[3] > 0 && counts[4] > 0 && counts[5] > 0 && counts[6] > 0 && counts[7] > 0 && counts[8] > 0 && counts[9] > 0) {
            i++;
            if (i < 10) {
                int a = i % 10;
                counts[a]--;
            }
            if (9 < i && i < 100) {
                int a = i % 10;
                counts[a]--;
                int b = i / 10 % 10;
                counts[b]--;
            }
            if (99 < i && i < 1000) {
                int a = i % 10;
                int b = i / 10 % 10;
                int c = i / 100 % 10;
                counts[b]--;
                counts[a]--;
                counts[c]--;
            }
            if (999 < i && i < 10000) {
                int a = i % 10;
                int b = i / 10 % 10;
                int c = i / 100 % 10;
                int d = i / 1000 % 10;
                counts[b]--;
                counts[a]--;
                counts[c]--;
                counts[d]--;
            }

        }
        System.out.println(i);
    }
}

C
在平面直角坐标系中,两点可以确定一条直线。如果有多点在一条直线上,
那么这些点中任意两点确定的直线是同一条。
给定平面上 2 × 3 个整点 {(x, y)|0 ≤ x < 2, 0 ≤ y < 3, x ∈ Z, y ∈ Z},即横坐标
是 0 到 1 (包含 0 和 1) 之间的整数、纵坐标是 0 到 2 (包含 0 和 2) 之间的整数
的点。这些点一共确定了 11 条不同的直线。
给定平面上 20 × 21 个整点 {(x, y)|0 ≤ x < 20, 0 ≤ y < 21, x ∈ Z, y ∈ Z},即横
坐标是 0 到 19 (包含 0 和 19) 之间的整数、纵坐标是 0 到 20 (包含 0 和 20) 之
间的整数的点。请问这些点一共确定了多少条不同的直线。
D
货物摆放
小蓝有一个超大的仓库,可以摆放很多货物。

现在,小蓝有 nn 箱货物要摆放在仓库,每箱货物都是规则的正方体。小蓝规定了长、宽、高三个互相垂直的方向,每箱货物的边都必须严格平行于长、宽、高。

小蓝希望所有的货物最终摆成一个大的长方体。即在长、宽、高的方向上分别堆 LL、WW、HH 的货物,满足 n = L \times W \times Hn=L×W×H。

给定 nn,请问有多少种堆放货物的方案满足要求。

例如,当 n = 4n=4 时,有以下 66 种方案:1×1×4、1×2×2、1×4×1、2×1×2、2 × 2 × 1、4 × 1 × 11×1×4、1×2×2、1×4×1、2×1×2、2×2×1、4×1×1。

请问,当 n = 2021041820210418n=2021041820210418 (注意有 1616 位数字)时,总共有多少种方案?

提示:建议使用计算机编程解决问题。

import java.util.HashSet;
import java.util.Set;
public class Main {
    static Long n = 2021041820210418L;
    static int aa = 0;

    public static void main(String[] args) {
        long end = (long)Math.sqrt(n);
//        System.out.println(end);
        //先求他的最小平方跟
        //之后求他的所有公因数
        Set<Long> div = new HashSet<>();
        for (long i = 1; i <= end; i++) {
            if (n % i == 0) {
                div.add(n / i);
                div.add(i);
            }
        }
        Long[] ls = div.toArray(new Long[0]);
        for (long a : ls) {
            for (long  b : ls) {
                for (long  c : ls) {
                    if (a * b * c == n) {
                        aa++;
                    }
                }
            }
        }
        System.out.println(aa);
    }
}

E
小蓝学习了最短路径之后特别高兴,他定义了一个特别的图,希望找到图 中的最短路径。

小蓝的图由 2021 个结点组成,依次编号 1 至 2021。

对于两个不同的结点 a, b,如果 a 和 b 的差的绝对值大于 21,则两个结点 之间没有边相连;如果 a 和 b 的差的绝对值小于等于 21,则两个点之间有一条 长度为 a 和 b 的最小公倍数的无向边相连。

例如:结点 1 和结点 23 之间没有边相连;结点 3 和结点 24 之间有一条无 向边,长度为 24;结点 15 和结点 25 之间有一条无向边,长度为 75。

请计算,结点 1 和结点 2021 之间的最短路径长度是多少。

public class Main {
    static int[][] graph = new int[2025][2025];
    static final int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        //先把所有的弄成最大的
        for (int i = 1; i <= 2021; i++) {
            for (int j = 1; j <= 2021; j++) {
                graph[i][j] = INF;
            }
        }
        for (int i = 1; i <= 2021; i++) {
            for (int j = 1; j <= 2021; j++) {
                if (j - i < 21 && j - i > 0) {
                    graph[i][j] = getMinCommonMultiple(i, j);
                    graph[j][i] = getMinCommonMultiple(i, j);
                }
            }
        }
        floyd();
        System.out.println(graph[2021][1]);
    }

    //使用辗转相除法
    public static int getMaxCommonDivisor(int a, int b) {
        //定义一个交换站值
        int temp = 0;
        while (a % b != 0) {
            temp = a % b;
            a = b;
            b = temp;
        }
        return b;
    }

    //两个数相乘等于这两个数的最大公约数和最小公倍数的 积
    public static int getMinCommonMultiple(int a, int b) {
        return a * b / getMaxCommonDivisor(a, b);
    }

    public static void floyd() {
        int len = 0;//变量保存距离
        //对中间顶点的遍历,k就是中间顶点的下标
        for (int k = 0; k <= 2021; k++) {
            //从i顶点出发
            for (int i = 0; i <= 2021; i++) {
                //到达j结点
                for (int j = 0; j <= 2021; j++) {
                    len = graph[i][k] + graph[k][j];//=>求出从i顶点出发,经过k中间顶点,到达j顶点的距离
                    //小于直连的值
                    if (len < graph[i][j]) {//如果len小于dis[i][j]距离
                        graph[i][j] = len;//更新len
                        graph[i][j] = k;//更新中间结点
                    }
                }
            }
        }
    }
}

F
小蓝要和朋友合作开发一个时间显示的网站。在服务器上,朋友已经获取 了当前的时间,用一个整数表示,值为从 1970 年 1 月 1 日 00:00:00 到当前时 刻经过的毫秒数。

现在,小蓝要在客户端显示出这个时间。小蓝不用显示出年月日,只需要 显示出时分秒即可,毫秒也不用显示,直接舍去即可。

给定一个用整数表示的时间,请将这个时间对应的时分秒输出。

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long l = Long.parseLong(scanner.nextLine());
        long a = l / 1000;
        long l1 = a % 60;
        long l2 = (a / 60) % 60;
        long l3 = (a / (60 * 60)) % 24;
        String s1;
        String s2;
        String s3;
        if (l1<10) {
             s1 = "0" + l1;
        }else {
             s1 = String.valueOf(l1);
        }
        if (l2<10) {
            s2 = "0" + l2;
        }else {
            s2 = String.valueOf(l2);
        }
        if (l3<10) {
            s3 = "0" + l3;
        }else {
            s3 = String.valueOf(l3);
        }
        System.out.println(s3+":"+s2+":"+s1);
    }
}

G
你有一架天平。现在你要设计一套砝码,使得利用这些砝码可以称出任意
小于等于 N 的正整数重量。
那么这套砝码最少需要包含多少个砝码?
注意砝码可以放在天平两边。
这里我们的思路,一个天平的状态有3种放左边,放右边,不放,既我们可以用一个三进制的树代表多大的数,

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Long num = Long.valueOf(scanner.nextLine());
        long sum = 0;
        long i = 1;
        long count = 0;
        while (num > sum) {
            sum +=i;
            i*=3;
            count += 1;
        }
        System.out.println(count);
    }
}

1 4 6
样例输出
10
暴力算法

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int[] ints = new int[i];
        for (int a = 0 ;a < ints.length; a++){
            ints[a] = scanner.nextInt();
        }
        Set<Integer> set = new HashSet<Integer>();
        //初始化set集合,放入0
        set.add(0);
        for (int a = 0; a < ints.length; a++) {
            List<Integer> ls = new ArrayList<Integer>(set);
            for (Integer integer : ls) {
                set.add(integer+ints[a]);
                set.add(Math.abs(integer - ints[a]));
            }
        }
        set.remove((Object)0);
        System.out.println(set.size());
    }
}

I

有一个n×n 的国际象棋棋盘(nn 行 nn 列的方格图),请在棋盘中摆放 nn 个受伤的国际象棋皇后,要求:

任何两个皇后不在同一行。
任何两个皇后不在同一列。
如果两个皇后在同一条 45 度角的斜线上,这两个皇后之间行号的差值至少为 3 。
请问一共有多少种摆放方案。

import java.util.*;

// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {

    static int count = 0;
    static int[] array;
    static int max;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        max = scanner.nextInt();
        array = new int[max];
        Nqueen(0);
        System.out.println(count);
        scanner.close();
    }

    //放置第n个皇后
    public static void Nqueen(int n) {
        if (max==n){
            count++;
            return;
        }
        for (int i = 0; i < max; i++) {
            array[n] = i;
            if (judge(n)) {
                Nqueen(n+1);
            }
        }
    }
    //查看皇后放到第n个
    public static boolean judge(int n){
        for (int i = 0; i < n; i++) {
            if (array[n] == array[i]) {
                return false;
            }
            if (Math.abs(n - i) == Math.abs(array[n] - array[i]) && n - i < 3){
                return false;
            }
        }
        return true;
    }
}

在这里插入图片描述

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        Integer[] ints = new Integer[n];
        for (int i = 0; i < n; i++) {
            ints[i] = i+1;
        }
        for (int i = 0; i < m; i++) {
            int p = scan.nextInt();
            int q = scan.nextInt();
            if (p == 0) {
                Arrays.sort(ints,0,q,Collections.reverseOrder());
            }else {
                Arrays.sort(ints,q-1,n);
            }
        }
        System.out.println(Arrays.toString(ints));
        scan.close();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值