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);
}
}
|