import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();//n个充电设备
int arry[] = new int[n];
for (int i = 0; i < n; i++) {
arry[i] = in.nextInt(); //不同功率的充电设备
}
int p_max = in.nextInt(); //充电站最大输出功率
int p_best = 0; //最优元素
int sum = 0;
//用双层循环计算充电设备的所有组合
for (int i = 0; i < arry.length; i++) {
for (int j = i; j < arry.length; j++) {
sum += arry[j];
if (sum <=p_max && sum > p_best) {
p_best = sum;
}
}
}
System.out.println("最优元素:"+p_best);
}
}
java:查找充电设备组合算法
于 2023-04-14 21:09:02 首次发布