-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (28 loc) · 834 Bytes
/
Main.java
File metadata and controls
35 lines (28 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int N= sc.nextInt();
int [] visited = new int[N];
int [] num = new int[N];
perm(N, visited, num, 0);
}
private static void perm(int n, int[] visited, int[] num, int r) {
if(r==n){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num.length; i++) {
sb.append(num[i]).append(" ");
}
System.out.println(sb);
}
for (int i = 0; i < n; i++) {
if(visited[i]==0){
visited[i]=1;
num[r]=i+1;
perm(n,visited,num,r+1);
visited[i]=0;
}
}
}
}