Skip to content

Commit a96724e

Browse files
committed
[Silver I] Title: 1, 2, 3 더하기 2, Time: 136 ms, Memory: 16332 KB -BaekjoonHub
1 parent 364adc3 commit a96724e

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int n, k, seq;
6+
static StringTokenizer st;
7+
static StringBuilder sb = new StringBuilder();
8+
static BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
9+
public static void main(String[] args) throws Exception{
10+
st = new StringTokenizer(br.readLine());
11+
12+
n = Integer.parseInt(st.nextToken());
13+
k = Integer.parseInt(st.nextToken());
14+
15+
recur(0, "");
16+
if(sb.length() == 0) sb.append("-1");
17+
System.out.println(sb);
18+
}
19+
20+
public static void recur(int cnt, String ans){
21+
if(cnt == n){
22+
if(seq == k){
23+
sb.append(ans.substring(1, ans.length()));
24+
}
25+
return;
26+
}
27+
28+
for(int i = 1; i < 4; i++){
29+
if(cnt + i == n) seq++;
30+
if(n < cnt + i) break;
31+
recur(cnt + i, ans + "+" + i);
32+
}
33+
}
34+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# [Silver I] 1, 2, 3 더하기 2 - 12101
2+
3+
[문제 링크](https://www.acmicpc.net/problem/12101)
4+
5+
### 성능 요약
6+
7+
메모리: 16332 KB, 시간: 136 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2025년 11월 15일 18:48:40
16+
17+
### 문제 설명
18+
19+
<p>정수 4를 1, 2, 3의 합으로 나타내는 방법은 총 7가지가 있다. 합을 나타낼 때는 수를 1개 이상 사용해야 한다.</p>
20+
21+
<ul>
22+
<li>1+1+1+1</li>
23+
<li>1+1+2</li>
24+
<li>1+2+1</li>
25+
<li>2+1+1</li>
26+
<li>2+2</li>
27+
<li>1+3</li>
28+
<li>3+1</li>
29+
</ul>
30+
31+
<p>이를 사전순으로 정렬하면 다음과 같이 된다.</p>
32+
33+
<ol>
34+
<li>1+1+1+1</li>
35+
<li>1+1+2</li>
36+
<li>1+2+1</li>
37+
<li>1+3</li>
38+
<li>2+1+1</li>
39+
<li>2+2</li>
40+
<li>3+1</li>
41+
</ol>
42+
43+
<p>정수 n과 k가 주어졌을 때, n을 1, 2, 3의 합으로 나타내는 방법 중에서 k번째로 오는 식을 구하는 프로그램을 작성하시오.</p>
44+
45+
### 입력
46+
47+
<p>첫째 줄에 정수 n과 k가 주어진다. n은 양수이며 11보다 작고, k는 2<sup>31</sup>-1보다 작거나 같은 자연수이다.</p>
48+
49+
### 출력
50+
51+
<p>n을 1, 2, 3의 합으로 나타내는 방법 중에서 사전 순으로 k번째에 오는 것을 출력한다. k번째 오는 식이 없는 경우에는 -1을 출력한다.</p>
52+

0 commit comments

Comments
 (0)