CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版

CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版



一、题目:

CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版

CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版

CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版



二、Java满分答案

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

public class Main {

    public static void main(String[] args) {

        Scanner scanner =new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        String str =scanner.nextLine();
        String[] num = str.split(" ");
        ArrayList<Integer> ints =new ArrayList<>();
        for (String x:num){
            ints.add(Integer.parseInt(x));
        }

        ArrayList<Integer> result = new ArrayList<>();
        int max=0;
        int resultMax =0;
        for (int i=0;i<n;i++){
            if(max>=ints.get(i)){
                result.add(max);
            }else {
                max=ints.get(i);
                result.add(max);
            }
            resultMax=resultMax+max;
        }

        result.clear();

        int min=0;
        int resultMin=0;
        for (int i=0;i<n;i++){
            if(min<ints.get(i)){
                min = ints.get(i);
                result.add(min);
            }else {
                result.add(0);
            }
            resultMin=resultMin+result.get(i);
        }

        System.out.println(resultMax);
        System.out.println(resultMin);
    }

}

上一篇:CSP:CSP认证考试:202109-2(非零段划分)70分答案,Java版


下一篇:python编程:布尔表达式与布尔代数笔记