标签:app link visit list 整数 sys scanner exception long
package lanqiao.improve; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; public class SumT { public static int cnt = 0, n = 0;//数量 public static int t = 0;//和 public static int[] nums;//数字 public static Stack<String> out = new Stack<>();//最后输出的栈 public static boolean isVisit[]; public static String toString(Iterator<Integer> it){ StringBuilder sb = new StringBuilder(); while (it.hasNext()){ sb.append(it.next()+" "); } return sb.toString(); } public static void dfs(int pos,int tempt,LinkedList<Integer> select,boolean isNone){ for(int i =pos;i>=0;i--){ if(!isVisit[i]){ isVisit[i] = true; select.push(nums[i]); dfs(i-1, tempt+nums[i],select ,false ); select.pop(); isVisit[i] = false; } } if(tempt == t && !isNone){ out.push(toString(select.iterator())); cnt++; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); n = sc.nextInt(); isVisit = new boolean[n]; nums = new int[n]; for(int i = 0;i<n;i++){ nums[i] = sc.nextInt(); } t = sc.nextInt(); sc.close(); dfs(n-1,0, new LinkedList<Integer>(),true); while (!out.isEmpty()){ System.out.println(out.pop()); } System.out.println(cnt); } }
标签:app link visit list 整数 sys scanner exception long
原文地址:https://www.cnblogs.com/mushuise/p/11637884.html