문제 링크: https://www.acmicpc.net/problem/1748
1748번: 수 이어 쓰기 1
첫째 줄에 N(1≤N≤100,000,000)이 주어진다.
www.acmicpc.net
120을 예로 들면, 1부터 120까지 120개의 수가 적어도 한자리씩은 차지한다. 10부터 120까지 120 - 9 = 111개의 수가 적어도 두자리씩은 차지한다. 그리고 100 부터 120까지 120 - 9 - 90 = 21개의 수가 적어도 세자리씩은 차지한다. 따라서 120 + 111 + 21 = 252라는 답을 구할수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int ans = 0;
int cnt = 9;
while(n > 0) {
ans += n;
n -= cnt;
cnt *= 10;
}
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
재귀함수 연습할겸 위의 로직을 이용하여 아래와 같이 구현해봤다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <bits/stdc++.h>
using namespace std;
int solve(int n, int cnt) {
if(n - cnt <= 0) return n;
return n + solve(n - cnt, cnt * 10);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
cout << solve(n, 9) << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'완전 탐색 (Brute Force)' 카테고리의 다른 글
백준 6064 카잉 달력 (0) | 2020.04.30 |
---|---|
백준 14500 테트로미노 (0) | 2020.04.29 |
백준 1107 리모컨 (0) | 2020.04.29 |
백준 1476 날짜 계산 (0) | 2020.04.29 |
백준 1018 체스판 다시 칠하기 (0) | 2020.04.24 |