package com.Time0905.ZhongXing;
import java.util.LinkedList;
import java.util.List;
public class Main2 {
public static void main(String[] args) {
//Scanner sc=new Scanner(System.in);
//int n= sc.nextInt();
int n = 8;
//int[] nums = new int[]{4, -4, 1, -3, 1, -3};
int[] nums = new int[]{4, -4, 6, -4, -3, -3,1,-1};
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> track = new LinkedList<>();
backtrack(nums, track, 0, res);
int resVal = 0;
for (int i = 0; i < res.size(); i++) {
resVal = Math.max(resVal, res.get(i).size());
if(res.get(i).size()==7){
for (int j = 0; j < res.get(i).size(); j++) {
System.out.print(res.get(i).get(j)+"->");
}
System.out.println("-----");
}
}
System.out.println(resVal);
//System.out.println("最后结果" + permute(nums,res));
}
public static void backtrack(int[] nums, LinkedList<Integer> track, int index, List<List<Integer>> res) {
//判断是否满足要求
// 如果元素都遍历完了,或者累加值<0了,那就返回false。
if (index >= nums.length || sumList(track) < 0) {
res.add(new LinkedList(track));
//既然满足了要求,那就返回吧
return;
}
//遍历
for (int i = index; i < nums.length; i++) {
// 现在队列里面加或不加元素,都不能继续走了
if (sumList(track) < 0) {
continue;
}
//加入元素
track.add(nums[i]);
//调用函数
backtrack(nums, track, i + 1, res);
//撤销选择
track.removeLast();
}
}
private static int sumList(LinkedList<Integer> track) {
int res = 0;
for (int i = 0; i < track.size(); i++) {
res += track.get(i);
}
return res;
}
}