Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions kikat/Permutation and Combination/AllPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package algorithm;

import java.util.Scanner;

public class AllPermutation {
public static StringBuilder stringBuilder = new StringBuilder();
public static int[] arr;
public static boolean[] isVisited;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

arr= new int[n];
isVisited = new boolean[n];

dfs(n, 0);
System.out.println(stringBuilder);
}

public static void dfs(int n , int depth) {
if(depth == n) {
for(int i : arr) {
stringBuilder.append(i).append(' ');
}
stringBuilder.append('\n');
return;
}

for(int i = 0; i < n; i++) {
if(!isVisited[i]) {
isVisited[i] = true;
arr[depth] = i + 1;
dfs(n, depth + 1);
isVisited[i] = false;
}
}
}
}
48 changes: 48 additions & 0 deletions kikat/Permutation and Combination/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package algorithm;

import java.util.Scanner;

public class Lotto {
public static int[] arr;
public static boolean[] isVisited;
public static int k;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while(true) {
k = scanner.nextInt();

if(k == 0) {
break;
}

arr = new int[k];
isVisited = new boolean[k];

for(int i = 0; i < k; i++) {
arr[i] = scanner.nextInt();
}
lotto(0, 0);
System.out.println();
}
}

public static void lotto(int start, int depth) {
if(depth == 6) {
for(int i = 0; i < k; i++) {
if(isVisited[i]) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
return;
}

for(int i = start; i < k; i++) {
isVisited[i] = true;
lotto(i + 1, depth + 1);
isVisited[i] = false;
}
}
}
34 changes: 34 additions & 0 deletions kikat/Permutation and Combination/NandMFour.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package algorithm;

import java.util.Scanner;

public class NandMFour {
public static int[] arr;
public static StringBuilder stringBuilder = new StringBuilder();

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int m = scanner.nextInt();

arr = new int[m];
dfs(n, m, 0, 0);
System.out.println(stringBuilder);
}

public static void dfs(int n , int m, int start, int depth) {
if(depth == m) {
for(int i : arr) {
stringBuilder.append(i).append(' ');
}
stringBuilder.append('\n');
return;
}

for(int i = start; i < n; i++) {
arr[depth] = i + 1;
dfs(n, m, i, depth + 1);
}
}
}
42 changes: 42 additions & 0 deletions kikat/Permutation and Combination/NandMOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package algorithm;

import java.util.Scanner;

public class NandMOne {

static boolean[] isVisited;
static int[] arr;
static StringBuilder stringBuilder = new StringBuilder();

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();

isVisited = new boolean[n];
arr = new int[m];

dfs(n,m, 0);

System.out.println(stringBuilder);
}

public static void dfs(int n, int m, int depth) {
if(depth == m) {
for(int i : arr) {
stringBuilder.append(i).append(' ');
}
stringBuilder.append('\n');
return;
}

for(int i = 0; i < n; i++) {
if(!isVisited[i]) {
isVisited[i] = true;
arr[depth] = i + 1;
dfs(n, m, depth + 1);
isVisited[i] = false;
}
}
}
}
35 changes: 35 additions & 0 deletions kikat/Permutation and Combination/NandMThree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package algorithm;

import java.util.Scanner;

public class NandMThree {
public static int[] arr;
public static StringBuilder stringBuilder = new StringBuilder();

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int m = scanner.nextInt();

arr = new int[m];

dfs(n, m, 0);
System.out.println(stringBuilder);
}

public static void dfs(int n, int m, int depth) {
if(m == depth) {
for(int i : arr) {
stringBuilder.append(i).append(' ');
}
stringBuilder.append('\n');
return;
}

for(int i = 0; i < n; i++) {
arr[depth] = i + 1;
dfs(n, m, depth + 1);
}
}
}
36 changes: 36 additions & 0 deletions kikat/Permutation and Combination/NandMTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package algorithm;

import java.util.Scanner;

public class NandMTwo {
public static int[] arr;
public static StringBuilder stringBuilder = new StringBuilder();

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int m = scanner.nextInt();

arr = new int[m];

dfs(n, m, 0, 0);

System.out.println(stringBuilder);
}

public static void dfs(int n, int m, int start, int depth) {
if(depth == m) {
for(int i : arr) {
stringBuilder.append(i).append(' ');
}
stringBuilder.append('\n');
return;
}

for(int i = start; i < n; i++) {
arr[depth] = i + 1;
dfs(n, m, i + 1, depth + 1);
}
}
}
52 changes: 52 additions & 0 deletions kikat/Permutation and Combination/NextPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package algorithm;

import java.util.Scanner;

public class NextPermutation {
public static int[] arr;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

arr = new int[n];

for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

if(nextPermutation()) {
for(int i : arr) {
System.out.print(i + " ");
}
} else {
System.out.println("-1");
}

}

private static boolean nextPermutation() {
int a = arr.length - 1;
while(a > 0 && arr[a - 1] >= arr[a]) a--;
if(a <= 0) return false;

int b = arr.length - 1;
while(arr[a - 1] >= arr[b]) b--;

int temp = arr[a - 1];
arr[a - 1] = arr[b];
arr[b] = temp;

int start = a;
int end = arr.length - 1;
while(start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return true;
}
}
56 changes: 56 additions & 0 deletions kikat/Permutation and Combination/PreviousPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package algorithm;

import java.util.Scanner;

public class PreviousPermutation {
public static int[] arr;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

arr = new int[n];

for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

if(previousPermutation()) {
for(int i : arr) {
System.out.print(i + " ");
}
} else {
System.out.println("-1");
}


}

public static boolean previousPermutation() {
int a = arr.length - 1;
while(a > 0 && arr[a - 1] <= arr[a]) a--;
if(a <= 0) {
return false;
}

// a - 1 이랑 그 다음으로 큰 수랑 바뀌어야 함.

int b = arr.length - 1;
while(b > 0 && arr[b] >= arr[a - 1]) b--;

int temp = arr[b];
arr[b] = arr[a - 1];
arr[a - 1] = temp;

int start = a;
int end = arr.length - 1;
while(start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return true;
}
}