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

P5724题目要求

P5724题目链接

在这里插入图片描述

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题目链接

在这里插入图片描述

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题目链接

在这里插入图片描述

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题目链接

在这里插入图片描述

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);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星拱北辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值