[BaekJoon] #33964 - 레퓨닛의 덧셈 [Java][C++]
[BaekJoon] #33964 - 레퓨닛의 덧셈 [Java][C++]
1. 아이디어
$X$자리 레퓨닛과 $Y$자리 레퓨닛의 합은 1이 자릿수의 차만큼 먼저 나오고 이후 2가 더 작은 자릿수만큼 나오게 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(X + Y)$ | $O(1)$ |
($X$ = 입력값 x, $Y$ = 입력값 y)
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
System.out.println("1".repeat(Math.abs(x - y)) + "2".repeat(Math.min(x, y)));
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x, y;
cin >> x >> y;
cout << string(abs(x - y), '1') << string(min(x, y), '2');
}
This post is licensed under CC BY 4.0 by the author.