CSP:CSP认证考试:202109-2(非零段划分)70分答案,Java版
作者:互联网
CSP:CSP认证考试:202109-2(非零段划分)70分答案,Java版
一、题目:
二、Java70分答案
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<>();
int max=0;
for (String x : num) {
if(max<=Integer.parseInt(x)){
max=Integer.parseInt(x);
}
ints.add(Integer.parseInt(x));
}
int notZeroMax=notZero(ints);
for (int y=1;y<=max;y++){
for (int z=ints.size()-1;z>=0;z--){
if(ints.get(z)<y){
ints.set(z,0);
}
}
if(notZeroMax<notZero(ints)){
notZeroMax=notZero(ints);
}
}
System.out.println(notZeroMax);
}
public static int notZero(ArrayList<Integer> numbers){
int n=numbers.size();
String str="";
for (int i=0;i<n;i++){
if (numbers.get(i)!=0){
while (numbers.get(i)!=0){
i++;
if(i==n){
break;
}
}
str=str+"#";
}
}
return str.length();
}
}
标签:Java,ints,int,非零段,numbers,str,notZeroMax,CSP,String 来源: https://blog.csdn.net/XRT_knives/article/details/121723321