20250826 baekjoon
This commit is contained in:
parent
78c684d640
commit
714cba765e
34
code_study/Baekjoon/java/_15666.java
Normal file
34
code_study/Baekjoon/java/_15666.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.util.*;
|
||||
|
||||
// 제출시 class 명을 Main으로 바꿀것!
|
||||
public class _15666 {
|
||||
static void dfs(int N, int M, int[] nums, int[] result, int current, int len){
|
||||
if(len == M) {
|
||||
for (int i=0; i<len; i++) {
|
||||
System.out.printf("%d ",result[i]);
|
||||
}
|
||||
System.out.printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int prev = -1;
|
||||
for(int i=current; i<N; i++){
|
||||
if(nums[i] != prev) {
|
||||
result[len] = nums[i];
|
||||
dfs(N,M,nums,result,i,len+1);
|
||||
prev = nums[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int[] line = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
|
||||
int N = line[0], M = line[1];
|
||||
int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
|
||||
Arrays.sort(nums);
|
||||
sc.close();
|
||||
|
||||
int[] result = new int[M];
|
||||
dfs(N,M,nums, result,0,0);
|
||||
}
|
||||
}
|
||||
29
code_study/Baekjoon/swift/15666.swift
Normal file
29
code_study/Baekjoon/swift/15666.swift
Normal file
@ -0,0 +1,29 @@
|
||||
func dfs(_ N: Int, _ M: Int, _ nums: [Int], _ current: Int, _ result: inout [Int]) {
|
||||
if result.count == M {
|
||||
for i in 0..<M {
|
||||
print(result[i], terminator: " ")
|
||||
}
|
||||
print()
|
||||
return
|
||||
}
|
||||
|
||||
var prev = -1
|
||||
for i in current..<N {
|
||||
if prev != nums[i] {
|
||||
result.append(nums[i])
|
||||
dfs(N,M,nums,i,&result)
|
||||
_ = result.popLast()
|
||||
prev = nums[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let line1 = readLine(), let NM = line1.split(separator: " ").compactMap({Int($0)}) as? [Int], NM.count == 2 {
|
||||
let N = NM[0]
|
||||
let M = NM[1]
|
||||
|
||||
if let line2 = readLine(), let nums = line2.split(separator: " ").compactMap({Int($0)}) as? [Int], nums.count == N {
|
||||
var result: [Int] = [];
|
||||
dfs(N,M,nums.sorted(),0,&result)
|
||||
}
|
||||
}
|
||||
25
code_study/Baekjoon/ts/15666.ts
Normal file
25
code_study/Baekjoon/ts/15666.ts
Normal file
@ -0,0 +1,25 @@
|
||||
export {};
|
||||
|
||||
function dfs(n:number, m:number, nums:number[], result:number[], current:number) {
|
||||
if(result.length === m) {
|
||||
console.log(...result);
|
||||
return;
|
||||
}
|
||||
|
||||
let prev = -1;
|
||||
for(let i=current; i<N; i++){
|
||||
if(nums[i] !== prev) {
|
||||
prev = nums[i];
|
||||
result.push(nums[i]);
|
||||
dfs(n,m,nums,result,i);
|
||||
result.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const input: string[] = require("fs").readFileSync(0,'utf8').toString().trim().split('\n');
|
||||
const [N, M]: number[] = input[0].split(' ').map(Number);
|
||||
let nums: number[] = input[1].split(' ').map(Number);
|
||||
nums.sort((a,b)=> a-b);
|
||||
|
||||
dfs(N,M,nums,[],0);
|
||||
Loading…
x
Reference in New Issue
Block a user