Post

[BaekJoon] 33964번 - 레퓨닛의 덧셈 [Java][C++]

[BaekJoon] 33964번 - 레퓨닛의 덧셈 [Java][C++]

문제 링크


1. 문제 풀이


$X$ 자리 레퓨닛과 $Y$ 자리 레퓨닛의 합은 $1$ 이 자릿수의 차만큼 먼저 나오고 이후 $2$ 가 더 작은 자릿수만큼 나오게 된다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)));
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    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.