Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
702 B
2
Indexable
Never
package ex1;

public class ex1 {
	static int[] A = new int [10];
	static int [] map = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	static int sum = 10;
	static int [] visit = new int [20];
	// in cac so co tong bang 10
	static void print (int n) {
		for (int i = 1; i < n; i++) {
			System.out.print(A[i]+" ");
		}
		System.out.println();
		
	}
	static void backtrack (int n, int sum) {
		if (sum > 10) return;
		if (sum == 10) {
			print(n);
			return;
		}
		for (int i = A[n-1]+1; i <= 10; i++) {
			if(visit[i] == 0) {
				visit[i] = 1;
				A[n] = i;
				backtrack(n+1, sum+i);
				visit[i] = 0;
			}
		}
	}
	
	public static void main(String[] args) {
		backtrack(1,0);
	}
}