题解 | 灵异背包?
灵异背包?
https://www.nowcoder.com/practice/812bcedbbe244c9b86e459a244af5ddf
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String n = in.nextLine();
int math = Integer.parseInt(n);
String number = in.nextLine();
String[]num = number.split(" ");
int res = 0;
List<Integer>list = new ArrayList<>();
int flag = 0;
for (int i = 0; i < math; i++) {
int a = Integer.parseInt(num[i]);
if (a % 2 == 0) {
res += a;
} else {
list.add(a);
}
}
list.sort((c, b) -> b - c);
if(list.size()%2!=0){
list.remove(list.size()-1);
}
int sum=list.stream().reduce(Integer::sum).orElse(0);
res+=sum;
System.out.println(res);
}
}
海康威视公司福利 1139人发布