Design a site like this with WordPress.com
Get started

liệt kê hoán vị của n phần tử

package practice;

import java.util.Scanner;

/*
* liệt kê hoán vị của n phần tử
*/
public class SinhHoanVi {
static int n;
static int[] arr;
static int[] visit;

static void result() {
for (int i = 1; i <= n; i++) {
System.out.print(arr[i] + ” “);
}
System.out.println();
}

static void Try(int index) {
for (int j = 1; j <= n; j++) {
if(visit[j] == 0) {
arr[index] = j;
visit[j] = 1;
if(index == n) result();
else Try(index + 1);
visit[j] = 0;
}
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
arr = new int[n + 1];
visit = new int[n + 1];
// có vẻ khởi tạo cấu hình đầu tiên hay không thì kết quả chạy vẫn thế
// for(int i=1; i<=n; i++) {
// arr[i] = i;
// }
Try(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: