【蓝桥杯练习系统】基础练习(Java)(普通试题数+VIP试题数)

“蓝桥杯”练习系统 (lanqiao.cn)

基础入门

BASIC-01 A+B问题 入门

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        System.out.println(A + B);
    }
}

BASIC-02 序列求和 入门 求和公式

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        System.out.println(n * (n + 1) / 2);
    }
}

BASIC-03 圆的面积 入门 实数输出

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        double area = Math.PI * r * r;
        System.out.printf("%.7f%n", area);
    }
}

BASIC-04 Fibonacci数列 入门 数列 取模

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), a = 1, b = 1, sum;
        for (int i = 3; i <= n; i++) {
            sum = (a + b) % 10007;
            a = b;
            b = sum;
        }
        System.out.println(b);
    }
}

基础练习

BASIC-1 闰年判断 条件判断

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0) ? "yes" : "no");
    }
}

BASIC-2 01字串 循环

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 32; i++) {
            String binaryString = Integer.toBinaryString(i);
            binaryString = String.format("%5s", binaryString).replace(' ', '0');
            System.out.println(binaryString);
        }
    }
}

BASIC-3 字母图形 循环 字符串

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = in.nextInt(), m = in.nextInt();
        for (int i = 0; i < n; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < m; j++) {
                sb.append((char) ('A' + Math.abs(j - i)));
            }
            out.println(sb);
        }
        out.flush();
    }
}

BASIC-4 数列特征 循环 最大值 最小值 累加

import java.io.*;

public class Main {
    static QuickInput in = new QuickInput();
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws IOException {
        int n = in.nextInt(), min = 10000, max = -10000, sum = 0;
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            min = Math.min(min, x);
            max = Math.max(max, x);
            sum += x;
        }
        out.println(max);
        out.println(min);
        out.println(sum);
        out.flush();
    }

    static class QuickInput {
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

BASIC-5 查找整数 循环 判断

import java.io.*;
import java.util.*;

public class Main {
    static QuickInput in = new QuickInput();

    public static void main(String[] args) throws IOException {
        int n = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            map.putIfAbsent(in.nextInt(), i);
        }
        System.out.println(map.getOrDefault(in.nextInt(), -1));
    }

    static class QuickInput {
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

BASIC-6 杨辉三角形 基础练习 二维数组

import java.io.*;
import java.util.Scanner;

public class Main {
    static int[][] arr;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = in.nextInt();
        arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                arr[i][j] = f(i, j);
                out.print(f(i, j) + " ");
            }
            out.println();
        }
        out.println();
        out.flush();
    }

    static int f(int i, int j) {
        if (arr[i][j] != 0) return arr[i][j];
        if (j == 0 || i == j) return 1;
        return f(i - 1, j - 1) + f(i - 1, j);
    }
}

BASIC-7 特殊的数字 循环 判断 数位

public class Main {
    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            if (i == (Math.pow(i / 100, 3) + Math.pow((i / 10) % 10, 3) + Math.pow(i % 10, 3))) {
                System.out.println(i);
            }
        }
    }
}

BASIC-8 回文数 循环 判断 回文数

import java.io.*;

public class Main {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        for (int i = 1000; i < 10000; i++) {
            String s = String.valueOf(i);
            if (s.equals(new StringBuilder(s).reverse().toString())) out.println(s);
        }
        out.flush();
    }
}

BASIC-9 特殊回文数 回文数 循环 条件语句

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = in.nextInt();
        for (int i = 10000; i < 1000000; i++) {
            String s = String.valueOf(i);
            StringBuilder sb = new StringBuilder(s);
            int num = 0;
            for (int j : sb.chars().toArray()) num += j - '0';
            if (num == n && s.equals(sb.reverse().toString())) out.println(s);
        }
        out.flush();
    }
}

BASIC-10 十进制转十六进制 循环 整除 求余 判断

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int res = Integer.parseInt(in.readLine());
        StringBuilder sb = new StringBuilder();
        if (res == 0) sb.append(0);
        while (res > 0) {
            int temp = res % 16;
            char c = (char) ('0' + temp);
            if (temp > 9) c = (char) ('A' + (temp - 10));
            sb = new StringBuilder(String.valueOf(c) + sb);
            res /= 16;
        }
        out.println(sb);
        out.flush();
    }
}

BASIC-11 十六进制转十进制 进制转换 字符处理 判断

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        String s = in.readLine();
        long res = 0;
        for (int i = 0; i < s.length(); i++) {
            int t;
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                t = s.charAt(i) - '0';
            } else {
                t = s.charAt(i) - 'A' + 10;
            }
            res += t * Math.pow(16, s.length() - i - 1);
        }
        out.println(res);
        out.flush();
    }
}

BASIC-12 十六进制转八进制 进制转换 字符 循环

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = Integer.parseInt(in.readLine());
        for (int i = 0; i < n; i++) {
            String s = in.readLine();
            StringBuilder temp = new StringBuilder();
            for (int j = 0; j < s.length(); j++) {
                switch (s.charAt(j)) {
                    case '0':
                        temp.append("0000");
                        break;
                    case '1':
                        temp.append("0001");
                        break;
                    case '2':
                        temp.append("0010");
                        break;
                    case '3':
                        temp.append("0011");
                        break;
                    case '4':
                        temp.append("0100");
                        break;
                    case '5':
                        temp.append("0101");
                        break;
                    case '6':
                        temp.append("0110");
                        break;
                    case '7':
                        temp.append("0111");
                        break;
                    case '8':
                        temp.append("1000");
                        break;
                    case '9':
                        temp.append("1001");
                        break;
                    case 'A':
                        temp.append("1010");
                        break;
                    case 'B':
                        temp.append("1011");
                        break;
                    case 'C':
                        temp.append("1100");
                        break;
                    case 'D':
                        temp.append("1101");
                        break;
                    case 'E':
                        temp.append("1110");
                        break;
                    case 'F':
                        temp.append("1111");
                        break;
                    default:
                        break;
                }
            }
            int mod = temp.length() % 3;
            if (mod == 1) temp = new StringBuilder("00" + temp);
            if (mod == 2) temp = new StringBuilder("0" + temp);
            for (int j = 0; j < temp.length(); j += 3) {
                int res = (temp.charAt(j) - '0') * 4 + (temp.charAt(j + 1) - '0') * 2 + (temp.charAt(j + 2) - '0');
                if (j == 0 && res == 0) continue;
                out.print(res);
            }
            out.println();
        }
        out.flush();
    }
}

BASIC-13 数列排序 数组 排序

import java.io.*;
import java.util.Arrays;

public class Main {
    static QuickInput in = new QuickInput();

    public static void main(String[] args) throws IOException {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        Arrays.sort(a);
        System.out.println(Arrays.toString(a).replaceAll("[\\[\\],]", ""));
    }

    static class QuickInput{
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

VIP试题

BASIC-14 时间转换 取余 数字字符混合输出

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt(); // 读取输入的时间(秒)
        int hours = t / 3600; // 计算小时
        int remainingSeconds = t % 3600; // 计算剩余的秒数
        int minutes = remainingSeconds / 60; // 计算分钟
        int seconds = remainingSeconds % 60; // 计算最终的秒数
        System.out.println(hours + ":" + minutes + ":" + seconds);
    }
}

BASIC-15 字符串对比 字符串 大小写

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine().trim();
        String str2 = scanner.nextLine().trim();
        // 比较字符串长度
        if (str1.length() != str2.length()) {
            System.out.println(1);
        } else if (str1.equals(str2)) {
            // 完全一致(区分大小写)
            System.out.println(2);
        } else if (str1.equalsIgnoreCase(str2)) {
            // 不区分大小写的情况下完全一致
            System.out.println(3);
        } else {
            // 即使不区分大小写也不能使这两个字符串一致
            System.out.println(4);
        }
    }
}

BASIC-16 分解质因数 质数分解 循环

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static List<Integer> sieveOfEratosthenes(int n) {
        List<Integer> primes = new ArrayList<>();
        boolean[] isPrime = new boolean[n + 1];
        for (int i = 2; i <= n; i++) isPrime[i] = true;
        for (int i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                primes.add(i);
                for (int j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return primes;
    }

    public static List<Integer> primeFactorization(int n, List<Integer> primes) {
        List<Integer> factors = new ArrayList<>();
        for (int prime : primes) {
            while (n % prime == 0) {
                factors.add(prime);
                n /= prime;
            }
        }
        if (n != 1) factors.add(n);
        return factors;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        List<Integer> primes = sieveOfEratosthenes(b);
        for (int i = a; i <= b; i++) {
            List<Integer> factors = primeFactorization(i, primes);
            System.out.print(i + "=");
            if (factors.isEmpty()) {
                System.out.print(i);
            } else {
                System.out.print(factors.get(0));
            }
            for (int j = 1; j < factors.size(); j++) {
                System.out.print("*" + factors.get(j));
            }
            System.out.println();
        }
    }
}

BASIC-17 矩阵乘法 二维数组 循环 矩阵

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        int[][] A = new int[N][N];

        // 读取矩阵A
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                A[i][j] = scanner.nextInt();
            }
        }

        // 计算A的M次幂
        int[][] result = matrixPower(A, M);

        // 输出结果
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] matrixMultiply(int[][] A, int[][] B) {
        int n = A.length;
        int[][] C = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        return C;
    }

    public static int[][] matrixPower(int[][] A, int M) {
        if (M == 0) {
            int n = A.length;
            int[][] identity = new int[n][n];
            for (int i = 0; i < n; i++) {
                identity[i][i] = 1;
            }
            return identity; // 返回单位矩阵
        }
        if (M % 2 == 1) {
            return matrixMultiply(A, matrixPower(A, M - 1));
        }
        int[][] halfPower = matrixPower(A, M / 2);
        return matrixMultiply(halfPower, halfPower);
    }
}

BASIC-18 矩形面积交 判断 线段交

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取第一个矩形的坐标
        double[] a1 = new double[4];
        for (int i = 0; i < 4; i++) a1[i] = scanner.nextDouble();

        // 读取第二个矩形的坐标
        double[] a2 = new double[4];
        for (int i = 0; i < 4; i++) a2[i] = scanner.nextDouble();

        // 计算相交矩形的左下和右上顶点坐标
        double[] a3 = new double[4];
        a3[0] = Math.max(Math.min(a1[0], a1[2]), Math.min(a2[0], a2[2]));
        a3[1] = Math.max(Math.min(a1[1], a1[3]), Math.min(a2[1], a2[3]));
        a3[2] = Math.min(Math.max(a1[0], a1[2]), Math.max(a2[0], a2[2]));
        a3[3] = Math.min(Math.max(a1[1], a1[3]), Math.max(a2[1], a2[3]));

        // 判断是否相交
        if (a3[0] > a3[2] || a3[1] > a3[3]) {
            System.out.print("0.00");
        } else {
            // 计算交集面积
            double area = Math.abs(a3[2] - a3[0]) * Math.abs(a3[3] - a3[1]);
            System.out.printf("%.2f", area);
        }
    }
}

BASIC-19 完美的代价 贪心算法

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        char[] s = scanner.nextLine().toCharArray();
        int swaps = findMinSwapsToPalindrome(s, n);
        if (swaps == -1) {
            System.out.println("Impossible");
        } else {
            System.out.println(swaps);
        }
    }

    private static int findMinSwapsToPalindrome(char[] s, int n) {
        int r = n - 1, cnt = 0, idx = 0;
        for (int i = 0; i < r; i++) {
            for (int j = r; j >= i; j--) {
                if (i == j){
                    if (n % 2 == 0 || idx == 1) {
                        return -1;
                    }
                    idx = 1;
                    cnt += n / 2 - i;
                }
                else if (s[i] == s[j]){
                    for (int l = j; l < r; l++) {
                        char t = s[l];
                        s[l] = s[l + 1];
                        s[l + 1] = t;
                        cnt++;
                    }
                    r--;
                    break;
                }
            }
        }
        return cnt;
    }
}

BASIC-20 数的读法 判断 函数

import java.util.Scanner;

public class Main {

    private static final String[] UNITS = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    private static final String[] PLACE_VALUES = {"", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi"};

    public static String numberToChinese(String num) {
        int n = num.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int digit = num.charAt(i) - '0';
            if (digit == 0) {
                if (n - i == 5 || n - i == 9) sb.append(" ");
                if (n - i != 1 && num.charAt(i + 1) != '0') sb.append("ling ");
            } else if ((n == 2 || n == 6 || n == 10) && digit == 1 && i == 0) {
                sb.append("shi ");
            } else {
                sb.append(UNITS[digit] + " " + PLACE_VALUES[n - i - 1] + " ");
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String num = scanner.next();
        scanner.close();
        String chinese = numberToChinese(num);
        System.out.println(chinese);
    }
}

BASIC-21 Sine之舞 字符串 递归 递推

import java.util.Scanner;

public class Main {
    private static String An(int n, int i) {
        if (i == n) return "sin(" + n + ")";
        if (i % 2 == 0) return "sin(" + i + "+" + An(n, i + 1) + ")";
        return "sin(" + i + "-" + An(n, i + 1) + ")";
    }

    private static String Sn(int n, int i) {
        if (n == 1) return An(n, 1) + "+" + i;
        return "(" + Sn(n - 1, i + 1) + ")" + An(n, 1) + "+" + i;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(Sn(n, 1));
    }
}

BASIC-22 FJ的字符串 字符串 递归

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        System.out.println(generateSequence(N));
    }

    public static String generateSequence(int N) {
        if (N == 1) {
            return "A";
        } else {
            char c = (char) (N - 1 + 'A');
            return generateSequence(N - 1) + c + generateSequence(N - 1);
        }
    }
}

BASIC-23 芯片测试 算法基础 统计 二维数组

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        QuickInput in = new QuickInput();
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = in.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            int ans = 0;
            for (int j = 0; j < n; j++) {
                if (arr[j][i] == 1) ans++;
            }
            if (ans > n / 2) out.print((i + 1) + " ");
        }
        out.flush();
    }

    static class QuickInput {
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

BASIC-24 龟兔赛跑预测 数组 模拟

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int v1 = scanner.nextInt(); // 兔子的速度
        int v2 = scanner.nextInt(); // 乌龟的速度
        int t = scanner.nextInt();  // 兔子领先t米时停止的时间
        int s = scanner.nextInt();  // 兔子休息的时间
        int l = scanner.nextInt();  // 赛道的长度

        int rabbitPosition = 0;
        int turtlePosition = 0;
        int time = 0;
        while(rabbitPosition < l && turtlePosition < l) {
            rabbitPosition += v1;
            turtlePosition += v2;
            time++;
            if (rabbitPosition == l || turtlePosition == l) {
                break;
            }
            if (rabbitPosition - turtlePosition >= t) {
                rabbitPosition -= v1 * s;
            }
        }
        if (rabbitPosition > turtlePosition) {
            System.out.println("R");
        } else if(turtlePosition > rabbitPosition) {
            System.out.println("T");
        } else {
            System.out.println("D");
        }
        System.out.println(time);
    }
}

BASIC-25 回形取数 二维数组 循环

import java.io.*;

public class Main {
    static QuickInput in = new QuickInput();
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) throws IOException {
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] matrix = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = in.nextInt();
            }
        }

        int i = 1, j = 0, cnt = 1;
        System.out.print(matrix[0][0]);
        matrix[0][0] = -1;
        while(m * n > cnt) {
            while (i < m && matrix[i][j] != -1) {
                out.print(" " + matrix[i][j]);
                matrix[i][j] = -1;
                i++;
                cnt++;
            }
            i--;
            j++;
            while (j < n && matrix[i][j] != -1) {
                out.print(" " + matrix[i][j]);
                matrix[i][j] = -1;
                j++;
                cnt++;
            }
            i--;
            j--;
            while (i >= 0 && matrix[i][j] != -1) {
                out.print(" " + matrix[i][j]);
                matrix[i][j] = -1;
                i--;
                cnt++;
            }
            i++;
            j--;
            while (j >= 0 && matrix[i][j] != -1) {
                out.print(" " + matrix[i][j]);
                matrix[i][j] = -1;
                j--;
                cnt++;
            }
            j++;
            i++;
        }
        out.flush();
    }

    static class QuickInput {
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

BASIC-26 报时助手 字符串 条件判断

import java.util.Scanner;

public class Main {
    private static final String[] UNITS = {"zero", "one", "two", "three", "four", "five",
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
            "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
            "eighteen", "nineteen"};
    private static final String[] PLACE_VALUES = {"twenty", "thirty", "forty", "fifty"};

    private static void f(int n) {
        if (n < 20) {
            System.out.print(UNITS[n] + " ");
        } else {
            System.out.print(PLACE_VALUES[n / 10 - 2] + " ");
            int t = n % 10;
            if (t > 0) System.out.print(UNITS[t] + " ");
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int h = scanner.nextInt();
        int m = scanner.nextInt();
        f(h);
        if (m == 0) System.out.println("o'clock");
        else f(m);
    }
}

BASIC-27 2n皇后问题 八皇后问题 搜索

import java.util.Scanner;

public class Main {
    static int[][] a = new int[10][10];
    static int[][] bj = new int[3][20];
    static int[][] bj1 = new int[3][20];
    static int n;
    static long sum = 0;

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

    public static void f2(int x) {
        for (int y = 0; y < n; y++) {
            if (a[x][y] == 1 && bj1[0][y] == 0 && bj1[1][x + y] == 0 && bj1[2][x - y + n] == 0) {
                a[x][y] = 3;
                bj1[0][y] = bj1[1][x + y] = bj1[2][x - y + n] = 1;
                if (x == n - 1) {
                    sum++;
                } else {
                    f2(x + 1);
                }
                bj1[0][y] = bj1[1][x + y] = bj1[2][x - y + n] = 0;
                a[x][y] = 1;
            }
        }
    }

    public static void f(int x) {
        for (int y = 0; y < n; y++) {
            if (a[x][y] == 1 && bj[0][y] == 0 && bj[1][x + y] == 0 && bj[2][x - y + n] == 0) {
                a[x][y] = 2;
                bj[0][y] = bj[1][x + y] = bj[2][x - y + n] = 1;
                if (x == n - 1) {
                    f2(0);
                } else {
                    f(x + 1);
                }
                bj[0][y] = bj[1][x + y] = bj[2][x - y + n] = 0;
                a[x][y] = 1;
            }
        }
    }
}

BASIC-28 Huffuman树 贪心 Huffuman

import java.io.*;
import java.util.PriorityQueue;

public class Main {
    static QuickInput in = new QuickInput();
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws IOException {
        int n = in.nextInt();
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            queue.add(in.nextInt());
        }
        long ans = 0;
        while (!queue.isEmpty()) {
            int a = queue.poll();
            if (queue.isEmpty()) break;
            a += queue.poll();
            ans += a;
            queue.add(a);
        }
        out.println(ans);
        out.flush();
    }

    static class QuickInput {
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }
    }
}

BASIC-29 高精度加法 数组 高精度

import java.io.*;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        BigInteger a = new BigInteger(String.valueOf(in.readLine()));
        BigInteger b = new BigInteger(String.valueOf(in.readLine()));
        out.println(a.add(b));
        out.flush();
    }
}

BASIC-30 阶乘计算 高精度

import java.io.*;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = Integer.parseInt(in.readLine());
        BigInteger ans = new BigInteger(String.valueOf(1));
        for (int i = 2; i <= n; i++) {
            BigInteger t = new BigInteger(String.valueOf(i));
            ans = ans.multiply(t);
        }
        out.println(ans);
        out.flush();
    }
}
  • 22
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值