Algorithm/BOJ

[BOJ]1748번: 수 이어 쓰기 1(c++)

HBGB 2020. 5. 3. 15:17

https://www.acmicpc.net/problem/1748

 

1748번: 수 이어 쓰기 1

첫째 줄에 N(1≤N≤100,000,000)이 주어진다.

www.acmicpc.net

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
 
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int num;
    cin >> num;
    
    int sum = 0;
    for (int start = 1, digit = 1; start <= num; start *= 10, digit++)
    {
        int end = start * 10 - 1;
        end = (end > num) ? num : end;
 
        sum += (end - start + 1* digit;
    }
 
    cout << sum;
 
    return 0;
}
Colored by Color Scripter

 

이렇게 저렇게 풀어봤지만

pow 등 라이브러리를 안쓰고

변수 하나 더 써서 푸는게 더 빠르다