[BaekJoon] #11944 - NN [Java][C++]
[BaekJoon] #11944 - NN [Java][C++]
1. 아이디어
$N$을 $N$번 출력하되 출력하는 문자열의 길이가 $M$자리를 넘어가면 $M$자리만 출력하는 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N \times D)$ | $O(N \times D)$ |
($N$ = 입력값 n, $D$ = n의 자릿수 $\approx \log_{10} n$)
3. 코드
풀이 [Java][C++]
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 s = String.valueOf(n).repeat(n);
System.out.println(s.substring(0, Math.min(s.length(), m)));
}
}
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(0);
cin.tie(0);
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.