View
λ°μν
- μμ΄κ³Ό μ‘°ν©μ μ°¨μ΄
λ½ν μ리 (μμμ μλ―Έ X )β μ‘°ν©
λ½ν μ리 (μμμ μλ―Έ 0 )β μμ΄
π‘ μ‘°ν©
nCr
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class CombinationTest {
static int N, R;
static int totalCnt;
static int[] arr, input;
// nCr: nκΉμ§μ μ μ€ rκ°λ₯Ό λͺ¨λ λ½μμ μμμκ² λμ΄ν¨(1<r<n)
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
totalCnt = 0;
input = new int[N];
arr = new int[R];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
input[i] = Integer.parseInt(st.nextToken());
}
com(0, 0);
System.out.println("μ΄ κ²½μ°μ μ :" + totalCnt);
}
// cnt+1λ²μ§Έ ν΄λΉνλ μ‘°ν©μμ ν¬ν¨λ μλ₯Ό λ½κΈ°
private static void com(int cnt, int start) {
if (cnt == R) {
totalCnt++;
System.out.println(Arrays.toString(arr));
return;
}
// κ°λ₯ν λͺ¨λ μμ λν΄μ νμ ->(input λ°°μ΄μ λͺ¨λ μλ₯Ό μλν΄μΌν¨ )
// start λΆν° μ²λ¦¬μ μ€λ³΅ μ μΆμΆ λ°©μ§ λ° μμκ° λ€λ₯Έ μ‘°ν© μΆμΆμ λ°©μ§ν¨
for (int i = start; i < N; i++) {
// start λΆν° μ²λ¦¬ -> μ€λ³΅μ²΄ν¬κ° νμ μμ΄μ§!
arr[cnt] = input[i];
// λ€μ μ λ½μΌλ¬
com(cnt + 1, i + 1);
}
}
}
λ°μν
'μκ³ λ¦¬μ¦' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] BFS / DFS μΈμ 리μ€νΈμ μ¬κ· (0) | 2022.08.30 |
---|---|
[Java] μμ΄/μ‘°ν©/λΆλΆμ§ν© (0) | 2022.08.30 |
[Java] μμ΄/μ‘°ν©/λΆλΆμ§ν© (0) | 2022.08.30 |
[Java] μμ΄(permutation) (0) | 2022.08.30 |
[Java] BFS(Breadth First Search) (0) | 2022.08.30 |
reply