20250822 baekjoon
This commit is contained in:
parent
3f8c5715f8
commit
84184b3ea4
31
code_study/Baekjoon/c/15652.c
Normal file
31
code_study/Baekjoon/c/15652.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void dfs(int n, int m, int now, int len, int* result);
|
||||
|
||||
int main(){
|
||||
int N, M;
|
||||
scanf("%d %d",&N, &M);
|
||||
|
||||
int* result = (int*)malloc(sizeof(int)*(M+1));
|
||||
dfs(N, M, 1, 0, result);
|
||||
free(result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dfs(int n, int m, int now, int len, int* result){
|
||||
if(len==m){
|
||||
for(int i=0; i<m; i++){
|
||||
printf("%d ",result[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for(int next = now; next<=n; next++){
|
||||
result[len++] = next;
|
||||
dfs(n, m, next, len, result);
|
||||
len--;
|
||||
}
|
||||
}
|
||||
51
code_study/Baekjoon/c/15654.c
Normal file
51
code_study/Baekjoon/c/15654.c
Normal file
@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void dfs(const int n, const int m, int len, int* result, bool* visited, const int* nums);
|
||||
int compare(const void *a, const void *b);
|
||||
|
||||
int main(){
|
||||
int N, M;
|
||||
scanf("%d %d",&N, &M);
|
||||
|
||||
int* result = (int*)malloc(sizeof(int)*M);
|
||||
int* nums = (int*)malloc(sizeof(int)*N);
|
||||
bool* visited = (bool*)malloc(sizeof(bool)*N);
|
||||
for(int i=0; i<N; i++) {
|
||||
scanf("%d",&nums[i]);
|
||||
visited[i] = false;
|
||||
}
|
||||
qsort(nums,N,sizeof(int), compare);
|
||||
dfs(N,M,0,result, visited, nums);
|
||||
free(result);
|
||||
free(visited);
|
||||
free(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dfs(const int n, const int m, int len, int* result, bool* visited, const int* nums) {
|
||||
if(len == m) {
|
||||
for(int i=0; i<m; i++){
|
||||
printf("%d ",result[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for(int next = 0; next<n; next++){
|
||||
if(!visited[next]) {
|
||||
visited[next] = true;
|
||||
result[len++] = nums[next];
|
||||
dfs(n,m,len,result,visited, nums);
|
||||
len--;
|
||||
visited[next] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b) {
|
||||
return *(int*)a - *(int*)b;
|
||||
}
|
||||
24
code_study/Baekjoon/swift/15652.swift
Normal file
24
code_study/Baekjoon/swift/15652.swift
Normal file
@ -0,0 +1,24 @@
|
||||
func dfs(_ n:Int, _ m:Int, _ now:Int, _ result:inout [Int]) {
|
||||
if result.count == m {
|
||||
for n in result {
|
||||
print(n, terminator: " ")
|
||||
}
|
||||
print()
|
||||
return
|
||||
}
|
||||
|
||||
for next in now...n {
|
||||
result.append(next)
|
||||
dfs(n,m,next,&result)
|
||||
_ = result.popLast()
|
||||
}
|
||||
}
|
||||
|
||||
if let input = readLine(),
|
||||
let nums:[Int] = input.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
||||
nums.count==2 {
|
||||
let N:Int = nums[0]
|
||||
let M:Int = nums[1]
|
||||
var result:[Int] = []
|
||||
dfs(N,M,1,&result)
|
||||
}
|
||||
30
code_study/Baekjoon/swift/15654.swift
Normal file
30
code_study/Baekjoon/swift/15654.swift
Normal file
@ -0,0 +1,30 @@
|
||||
func dfs(_ n:Int, _ m:Int, _ visited:inout [Bool], _ result:inout [Int], _ nums:[Int]) {
|
||||
if result.count == m {
|
||||
for n in result {
|
||||
print(n, terminator: " ")
|
||||
}
|
||||
print()
|
||||
return
|
||||
}
|
||||
|
||||
for next in 0..<n {
|
||||
if !visited[next] {
|
||||
visited[next] = true
|
||||
result.append(nums[next])
|
||||
dfs(n,m,&visited,&result,nums)
|
||||
_ = result.popLast()
|
||||
visited[next] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let input1 = readLine(), let NM:[Int] = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], NM.count == 2 {
|
||||
let N:Int = NM[0]
|
||||
let M:Int = NM[1]
|
||||
|
||||
if let input2 = readLine(), let nums:[Int] = input2.split(separator: " ").compactMap({Int($0)}) as? [Int], nums.count == N {
|
||||
var visited:[Bool] = Array(repeating: false, count: N)
|
||||
var result:[Int] = []
|
||||
dfs(N, M, &visited, &result, nums.sorted())
|
||||
}
|
||||
}
|
||||
17
code_study/Baekjoon/ts/15652.ts
Normal file
17
code_study/Baekjoon/ts/15652.ts
Normal file
@ -0,0 +1,17 @@
|
||||
export {};
|
||||
|
||||
function dfs(n:number, m:number, now:number, result:number[]) {
|
||||
if(result.length === m) {
|
||||
console.log(...result);
|
||||
return;
|
||||
}
|
||||
|
||||
for(let next = now; next<=n; next++) {
|
||||
result.push(next);
|
||||
dfs(n,m,next,result);
|
||||
result.pop();
|
||||
}
|
||||
}
|
||||
|
||||
const [N, M]: number[] = require("fs").readFileSync(0, "utf8").toString().trim().split(' ').map(Number);
|
||||
dfs(N, M, 1, []);
|
||||
25
code_study/Baekjoon/ts/15654.ts
Normal file
25
code_study/Baekjoon/ts/15654.ts
Normal file
@ -0,0 +1,25 @@
|
||||
export {};
|
||||
|
||||
function dfs(n:number, m:number, nums:number[], visited:boolean[], result:number[]){
|
||||
if(result.length === m) {
|
||||
console.log(...result);
|
||||
return;
|
||||
}
|
||||
|
||||
for(let next=0; next<n; next++){
|
||||
if(!visited[next]) {
|
||||
visited[next] = true;
|
||||
result.push(nums[next]);
|
||||
dfs(n, m, nums, visited, result);
|
||||
result.pop()
|
||||
visited[next] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const input: string[] = require("fs").readFileSync(0, "utf8").toString().trim().split('\n');
|
||||
const [N,M]: number[] = input[0].split(' ').map(Number);
|
||||
const nums: number[] = input[1].split(' ').map(Number);
|
||||
let visited:boolean[] = Array.from({length: N}, () => false);
|
||||
nums.sort((a,b)=>a-b);
|
||||
dfs(N, M, nums, visited, []);
|
||||
Loading…
x
Reference in New Issue
Block a user