洛谷入门题P1046、P1047、P1427、P1428、P2141、P1567题解(Java语言描述)

P1046题目要求

P1046题目链接

在这里插入图片描述

P1046题解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int counter = 0;
        int[] array = new int[10];
        for (int i = 0; i < 10; i++) {
            array[i] = scanner.nextInt();
        }
        int height = scanner.nextInt() + 30;
        for (int i = 0; i < 10; i++) {
            if (array[i] <= height) {
                counter++;
            }
        }
        System.out.println(counter);
        scanner.close();
    }
}

P1047题目要求

P1047题目链接

在这里插入图片描述

P1047题解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int lineNum = scanner.nextInt();
        int counter = 0;
        //注意树维L+1棵
        int[] array = new int[length+1];
        for (int i = 0; i < lineNum; i++) {
            int from = scanner.nextInt(), to = scanner.nextInt();
            for (int j = from; j <= to; j++) {
                array[j] = 1;
            }
        }
        for (int i = 0; i <= length; i++) {
            if (array[i] == 0) {
                counter++;
            }
        }
        System.out.println(counter);
        scanner.close();
    }
}

P1427题目要求

P1427题目链接

在这里插入图片描述

P1427题解

以前写的,竟然还用了ArrayList……

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

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> array = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()) {
            int x = scan.nextInt();
            if (x != 0) {
                array.add(x);
            } else {
                break;
            }
        }
        int length = array.size();
        for (int i = length-1; i >= 0; i--) {
            System.out.print(array.get(i) + " ");
        }
        scan.close();
    }

}

P1428题目要求

P1428题目链接

在这里插入图片描述

P1428题解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] array = new int[n];
        int[] result = new int[n];
        int m;
        int count;
        for (int i = 0; i < n; i++) {
            m = scan.nextInt();
            array [i] = m;
            count = 0;
            for (int j = i; j>=0; j--) {
                if (array [i] > array [j]) {
                    count++;
                }
            }
            result[i] = count;
        }
        for (int i : result) {
            System.out.print(i + " ");
        }
        scan.close();
    }

}

P2141题目要求

P2141题目链接

在这里插入图片描述
在这里插入图片描述

P2141题解

这题小麻烦一些,解题历程

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int[] array = new int[num];
        for (int i = 0; i < num; i++) {
            array[i] = scanner.nextInt();
        }
        int[] counterArr = new int[num];
        for (int i = 0; i < num; i++) {
            int now = array[i];
            for (int j = 0; j < num; j++) {
                if (j == i) {
                    continue;
                }
                int num1 = array[j];
                for (int k = 0; k < num; k++) {
                    if (k == i || k == j) {
                        continue;
                    }
                    int num2 = array[k];
                    if (num1 + num2 == now) {
                        counterArr[i] = 1;
                        break;
                    }
                }
            }
        }
        int counter = 0;
        for (int i = 0; i < num; i++) {
            if (counterArr[i] == 1) {
                counter++;
            }
        }
        System.out.println(counter);
        scanner.close();
    }
}

P1567题目要求

P1567题目链接

在这里插入图片描述

P1567题解

这个题太难了,性能卡的太死了,1s+125MB的双限制在本题中,对Java用户来说太难了!!!!!!强烈谴责!!!
辛酸的解题历程

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int num = reader.read();
        reader.readLine();
        String[] arr = reader.readLine().split(" ");
        int counter = 0;
        int max = 0;
        for (int i = 1; i < arr.length; i++) {
            int tem1 = Integer.parseInt(arr[i]);
            int tem2 = Integer.parseInt(arr[i-1]);
            if((tem1 - tem2) > 0) {
                counter++;
            } else {
                max=Math.max(max, counter);
                counter = 0;
            }
        }
        System.out.println(max+1);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星拱北辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值