[BaekJoon] 11944번 - NN [Java][C++]
[BaekJoon] 11944번 - NN [Java][C++]
1. 문제 풀이
$N$ 을 $N$ 번 출력하되 출력하는 문자열의 길이가 $M$ 자리를 넘어가면 $M$ 자리만 출력하는 문제다. $N$ 을 $N$ 번 반복한 문자열을 만들고 문자열의 길이와 $M$ 중 더 짧은 값만큼만 출력해줬다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
String str = String.valueOf(N).repeat(N);
System.out.println(str.substring(0, Math.min(str.length(), M)));
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
string s = to_string(n);
string res;
for (int i = 0; i < n; i++) {
res += s;
}
for (int i = 0; i < min((int)res.size(), m); i++) {
cout << res[i];
}
}
This post is licensed under CC BY 4.0 by the author.