1. 题目描述
收银员现有 n 张面值分别为 v1,v2,…,vn 的纸币。若找零金额为 m,则一共有多少种找零方法?
注:0<n≤1000,0<v1,v2,…,vn≤10000,0<m≤10000
2.输入格式
n v1 ,v2,...,vn m
3. 输出格式
若有解,则输出全部找零方案,每输出一种 若无解,则输出“None”
4. 输入样例1
6
3 1 4 3 2 7
9
5.输出样例1
3 1 3 2
3 4 2
4 3 2
2 7
6. 输入样例2
5
5 3 4 6 7
2
7.输出样例2
None
import java.util.Scanner;
public class Main {
static int a[];
static int x[]; //解向量
static int c, n ,cnt = 0; // c目标 cnt标记是否输出None
public static void dfs(int i, int c, int s) {
if(s == c) {
for(int k = 0; k < i; k++)
if(x[k] == 1)
System.out.print( a[k]+" ");
System.out.println(a[i]);
cnt++;
}else {
for(; i < n; i++) {
if(x[i] == 0 && s + a[i] <= c) {
x[i] = 1; // 置为已访问
s += a[i];
dfs(i, c, s);
x[i] = 0; // 回溯
s -= a[i];
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new int[n];
x = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
x[i] = 0;
}
c = sc.nextInt();
dfs(0, c, 0);
if(cnt == 0)
System.out.println("None");
}
}