2020第十一届蓝桥杯真题JAVA B组省赛答案分享(2020.10.17)

试题 A: 门牌制作

这题,感觉还行。

我的答案:624

public class Main1 {
    
    
    public static void main(String[] args) {
    
    
        int n = 2020, sum = 0;
        for (int i = 1; i <= 1000; i++) {
    
    
            for (int j = 0, k = 1; j < String.valueOf(i).length(); j++, k *= 10) {
    
    
                int m = i % (k * 10) / k;
                if (m == 2) {
    
    
                    sum ++;
                }
            }
        }
        System.out.println(sum);
    }
}

试题 B: 寻找 2020

这题就是录入数据的时候有点麻烦,思路挺清晰的,先一行一行找,再一列一列找,最后找斜线的,就是注意控制下标越界。

我的答案:16520

public class Main2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), sum = 0;
        int[][] a = new int[n][n];
        sc.nextLine();
        for (int i = 0; i < n; i++) {
    
    
            String s = sc.nextLine();
            String[] str = s.split("");
            for (int j = 0; j < n; j++) {
    
    
                a[i][j] = Integer.parseInt(str[j]);
            }
        }

        // 行和列
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < n - 3; j++) {
    
    
                int a1 = a[i][j] * 1000 + a[i][j+1] * 100 + a[i][j+2] * 10 + a[i][j+3];
                if (a1 == 2020) {
    
    
                    sum ++;
                }
                int a2 = a[j][i] * 1000 + a[j+1][i] * 100 + a[j+2][i] * 10 + a[j+3][i];
                if (a2 == 2020) {
    
    
                    sum ++;
                }
            }
        }

        // 斜线
        for (int i = 0; i < n-3; i++) {
    
    
            for (int j = 0; j < n - 3; j++) {
    
    
                int a1 = a[i][j] * 1000 + a[i+1][j+1] * 100 + a[i+2][j+2] * 10 + a[i+3][j+3];
                if (a1 == 2020) {
    
    
                    sum ++;
                }
            }
        }
        System.out.println(sum);
    }
}

试题 C: 蛇形填数

这题仔细观察还是有规律的,我下面的代码是找左上到右下斜线上的数。

public class Main3 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), sum = 0;
        for (int i = 1; i <= n + n - 1; i++) {
    
    
            sum += i;
        }
        System.out.println(sum - (n - 1));
    }
}

试题 D: 七段码

这题题目看错,据说答案是:80。

试题 E: 排序

这题我有点懵,我也算了个答案出来:mnlkojihgfedcba,不知对不对,我感觉不妙。

试题 F: 成绩分析

这题,怎么说呢,感觉…没什么感觉。

public class Main6 {
    
    

    static int n;

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
    
    
            a[i] = sc.nextInt();
        }
        f(a);
    }

    static void f(int[] a) {
    
    
        int max = a[0], min = a[0];
        double sum = 0.0;
        for (int i = 0; i < n; i++) {
    
    
            if (max < a[i])
                max = a[i];

            if (min > a[i])
                min = a[i];

            sum += a[i];
        }
        System.out.println(max);
        System.out.println(min);
        System.out.printf("%.2f", sum / n);
    }
}

试题 G: 单词分析

这道题思路清晰,我的是这么想的,双指针遍历,右指针定位,左指针查找前面的并计算个数,若个数超过前面的就替换。

public class Main7 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        f(s);
    }

    static void f(String s) {
    
    
        if (s.length() == 1) {
    
    
            System.out.println(s);
            System.out.println(s.length());
        }
        char m = s.charAt(0);
        int count = 1;
        for (int i = 1; i < s.length(); i++) {
    
    
            if (s.charAt(i) == m) {
    
    
                count ++;
            } else {
    
    
                int num = 1;
                for (int j = 0; j < i; j++) {
    
    
                    if (s.charAt(j) == s.charAt(i)) {
    
    
                        num ++;
                    }
                }
                if (num > count) {
    
    
                    m = s.charAt(i);
                    count = num;
                }
            }
        }
        System.out.println(m);
        System.out.println(count);
    }
}

试题 H: 数字三角形

这题,我想哭死了,看错题目,只算出每次查找最大的,没注意看到“左右向下次数的差不超过1”,20分直接没了。思路就dfs吧,额。。感觉dfs每年都会考,就控制往下和往右下走,记录往下走和往右下走的次数,在回溯的时候减一。

public class Main8 {
    
    

    private static int N, sum = 0, l = 0, r = 0;
    private static int[][] a, xy = {
    
    {
    
    1, 0}, {
    
    1, 1}};
    private static int[] b;

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        sc.nextLine();
        a = new int[N][N];
        b = new int[N];
        for (int i = 0; i < N; i++) {
    
    
            String[] s = sc.nextLine().split(" ");
            for (int j = 0; j < i + 1; j++) {
    
    
                a[i][j] = Integer.parseInt(s[j]);
            }
        }

        f(0, 0, 0);
        System.out.println(sum);
    }

    static void f(int x, int y, int index) {
    
    
        if (index == N - 1) {
    
    
            if (Math.abs(l - r) <= 1) {
    
    
                sum = sumF(b);
            }
            return;
        }
        for (int i = 0; i < xy.length; i++) {
    
    
            int o = x + xy[i][0];
            int p = y + xy[i][1];
            if (o < N && p < N) {
    
    
                if (i == 0)
                    l ++;
                else
                    r ++;
                b[index] = a[o][p];
                f(o, p, 1 + index);
                if (i == 0)
                    l --;
                else
                    r --;
            }
        }
    }

    static int sumF(int[] arrb) {
    
    
        int sum1 = a[0][0];
        for (int i = 0; i < N - 1; i++) {
    
    
            sum1 += arrb[i];
        }
        if (sum1 > sum)
            sum = sum1;
        return sum;
    }
}

试题 I: 子串分值和

这题做出来了,但是时间复杂度有点高n^3,这里就麻烦广大的网友们帮我优化一下了。

public class Main9 {
    
    

    static Set<String> set = new HashSet<String>();
    static int sum = 0;

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();

        for (int i = 0; i < s.length(); i++) {
    
    
            for (int j = i; j < s.length(); j++) {
    
    
                sum += num(s.substring(i, j + 1));
            }
        }
        System.out.println(sum);
    }

    static int num(String str) {
    
    
        set.clear();
        for (int i = 0; i < str.length(); i++) {
    
    
            set.add(str.substring(i, i + 1));
        }
        return set.size();
    }
}

试题 J: 装饰珠

放弃

猜你喜欢

转载自blog.csdn.net/Yh_yh_new_Yh/article/details/109144263