洛谷入门题P1046、P1047、P1427、P1428、P2141、P1567题解(Java语言描述)
生活随笔
收集整理的這篇文章主要介紹了
洛谷入门题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用戶來說太難了!!!!!!強烈譴責!!!
辛酸的解題歷程
總結
以上是生活随笔為你收集整理的洛谷入门题P1046、P1047、P1427、P1428、P2141、P1567题解(Java语言描述)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据结构与算法】比较法分析查找算法与查
- 下一篇: java美元兑换,(Java实现) 美元