Design a site like this with WordPress.com
Get started

Liệt kê tập con – backtracking

package practice;

import java.util.Scanner;

public class LietKeTapCon {
static int n;
static int []a = new int[100];

static void Show() {
for (int i = 1; i <= n; i++) {
if (a[i] != 0) {
System.out.print(i+” “);
}
}
System.out.println();
}

static void backTrack(int index) {
if (index == n + 1) {
Show();
return;
}

a[index] = 0;
backTrack(index + 1);
a[index] = 1;
backTrack(index + 1);

}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
backTrack(1);
}
}

Advertisement

Author: alexishuuuocn

I'm a software engineer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: