Blue Bridge Cup 31-day sprint punch-in problem solution (Day25)

Day25

first question

The 11th 2020 Blue Bridge Cup Provincial Championship

performance statistics

Check-in questions, simulations, Math.round()rounding using functions.

import java.util.Scanner;

public class Main {
    
    

    static final int N = 10010;
    static int[] a = new int[N];

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double cnt1 = 0, cnt2 = 0;
        for (int i = 0; i < n; i++) {
    
    
            a[i] = sc.nextInt();
            if (a[i] >= 60) cnt1++;
            if (a[i] >= 85) cnt2++;
        }

        System.out.println(Math.round((cnt1 / n) * 100) + "%");
        System.out.println(Math.round((cnt2 / n) * 100) + "%");
    }
}

Question 2

The 11th 2020 Blue Bridge Cup Provincial Championship

abbreviated fraction

C++B组第2题

填空题

Enumerate double forloops with gcdsolve.

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int cnt = 0;
        for (int i = 1; i <= 2020; i++) {
    
    
            for (int j = 1; j <= 2020; j++) {
    
    
                if (gcd(i, j) == 1) cnt++;
            }
        }

        System.out.println(cnt);
    }

    private static int gcd(int a, int b) {
    
    
        return b != 0 ? gcd(b, a % b) : a;
    }
}

Question 3

The 10th 2019 Blue Bridge Cup National Championship

optimal inclusion

C++B组第6题

DP

This is a problem with the shortest edit distance of linear DP, but there are no insertion and deletion operations, only replacement, refer to the AcWing problem solution .

import java.io.*;

public class Main {
    
    

    static final int N = 1010, INF = 100000; // 此处用一个较大的整数表示无穷
    static int[][] dp = new int[N][N];

    public static void main(String[] args) throws IOException {
    
    
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        String t = in.readLine();
        int n = s.length(), m = t.length();
        /*
            dp这个二维数组中,竖列代表s,横列代表t,t比s短,到后面会有很多空串,
            无论怎么修改都不会使空串变长,故先全部初始化为无穷;
            与之区别。s先全部初始化为0
            注意代码 先后顺序 ,要保证dp[0][0]=0
        */
        for (int i = 0; i <= m; i++) dp[0][i] = INF;
        for (int i = 0; i <= n; i++) dp[i][0] = 0;

        // 查改过程 
        for (int i = 1; i <= n; i++) {
    
    
            for (int j = 1; j <= m; j++) {
    
    
                dp[i][j] = dp[i - 1][j]; // 如果s的前i个包括t的前j个,那么s的前i-1个应该也包括t的前j个
                if (s.charAt(i - 1) == t.charAt(j - 1))
                    // 这里和其它答案写得不太一样,但是不减一的话会导致下标超出范围值
                    dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i][j]); // 相当于不作处理直接跳过
                else
                    dp[i][j] = Math.min(dp[i - 1][j - 1] + 1, dp[i][j]); // 操作数加一
                	// 储存着整个s和t的最小编辑距离
            }
        }
        
        System.out.println(dp[n][m]);
    }
}

おすすめ

転載: blog.csdn.net/weixin_53407527/article/details/123908761