洛谷P5724、P5727、P5728、P5729题题解(Java语言描述)

P5724题目要求

P5724题目链接

洛谷P5724、P5727、P5728、P5729题题解(Java语言描述)_第1张图片

P5724分析

存一下max和min,max-min就是极差……

P5724AC代码(Java语言描述)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int max = scanner.nextInt(), min = max;
        for (int i = 1; i < num; i++) {
            int temp = scanner.nextInt();
            if (temp > max) {
                max = temp;
            }
            if (temp < min) {
                min = temp;
            }
        }
        scanner.close();
        System.out.println(max - min);
    }
}

P5727题目要求

P5727题目链接

洛谷P5724、P5727、P5728、P5729题题解(Java语言描述)_第2张图片

P5727分析

按照规矩来呗……都说的很明白了……

P5727AC代码(Java语言描述)

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        scanner.close();
        LinkedList<Integer> list = new LinkedList<>();
        while (num > 1) {
            list.push(num);
            if (num % 2 == 0) {
                num /= 2;
            } else {
                num*=3;
                ++num;
            }
        }
        list.push(1);
        for (int i = 0; i < list.size()-1; i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println(list.get(list.size()-1));
    }
}

P5728题目要求

P5728题目链接

洛谷P5724、P5727、P5728、P5729题题解(Java语言描述)_第3张图片

P5728分析

排列组合一下,按照要求判断一下就行……

P5728AC代码(Java语言描述)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        int[] chinese_array = new int[num], math_array = new int[num],
                english_array = new int[num], grade_array = new int[num];
        for (int i = 0; i < num; i++) {
            String str = scanner.nextLine();
            String[] arr = str.split(" ");
            chinese_array[i] = Integer.parseInt(arr[0]);
            math_array[i] = Integer.parseInt(arr[1]);
            english_array[i] = Integer.parseInt(arr[2]);
            grade_array[i] = chinese_array[i] + math_array[i] + english_array[i];
        }
        int counter = 0;
        for (int i = 0; i < num; i++) {
            for (int j = i+1; j < num; j++) {
                if (Math.abs(chinese_array[i]-chinese_array[j]) <= 5 &&
                        Math.abs(math_array[i]-math_array[j]) <= 5 &&
                        Math.abs(english_array[i]-english_array[j]) <= 5 &&
                        Math.abs(grade_array[i]-grade_array[j]) <= 10) {
                    counter++;
                }
            }
        }
        scanner.close();
        System.out.println(counter);
    }
}

P5729题目要求

P5729题目链接

洛谷P5724、P5727、P5728、P5729题题解(Java语言描述)_第4张图片

P5729分析

数据不大,开个三维数组,每次就切一下就行哈哈哈哈哈……

P5729AC代码(Java语言描述)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        byte x = scanner.nextByte(), y = scanner.nextByte(), z = scanner.nextByte(), num = scanner.nextByte();
        byte[][][] cuboid = new byte[x+1][y+1][z+1];
        for (byte i = 0; i < num; i++) {
            byte x1 = scanner.nextByte(), y1 = scanner.nextByte(), z1 = scanner.nextByte();
            byte x2 = scanner.nextByte(), y2 = scanner.nextByte(), z2 = scanner.nextByte();
            for (byte j = x1; j <= x2; j++) {
                for (byte k = y1; k <= y2; k++) {
                    for (byte l = z1; l <= z2; l++) {
                        cuboid[j][k][l] = 1;
                    }
                }
            }
        }
        scanner.close();
        int counter = 0;
        for (byte i = 1; i <= x; i++) {
            for (byte j = 1; j <= y; j++) {
                for (byte k = 1; k <= z; k++) {
                    if (cuboid[i][j][k] == 0) {
                        counter++;
                    }
                }
            }
        }
        System.out.println(counter);
    }
}

你可能感兴趣的:(#,Algorithm-LuoGu)