Algorithm/백준
[백준] 1748번 수 이어쓰기1
sirius
2021. 3. 2. 11:21
https://www.acmicpc.net/problem/1748
1748번: 수 이어 쓰기 1
첫째 줄에 N(1 ≤ N ≤ 100,000,000)이 주어진다.
www.acmicpc.net
자릿수가 바뀌는 기준 확인
- 1 ~ 9 : 1자리
- 10 ~ 99 : 2자리
- 100 ~ 999 : 3자리
- 1000 ~ 9999 : 4자리
i를 10, 100, 1000, ...으로 나눴을 때 나머지가 0이면 더해야 하는 자릿수를 올려준다.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ans = 0;
int count = 1; // 더해야 하는 자릿수
int num = 10;
int N = sc.nextInt();
for(int i = 1 ; i <= N; i++) {
// 자릿수 변경( 10, 100, 1000 ... )
if(i % num == 0) {
num *= 10;
count++;
}
ans += count;
}
System.out.println(ans);
}
}