알고리즘/초급1
[JAVA] 1748번: 수 이어쓰기1 ( 초급 2 - 7 )
daram
2021. 9. 23. 12:47
문제
1부터 N까지의 수를 이어서 쓰면 다음과 같이 새로운 하나의 수를 얻을 수 있다.
1234567891011121314151617181920212223...
이렇게 만들어진 새로운 수는 몇 자리 수일까? 이 수의 자릿수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 N(1 ≤ N ≤ 100,000,000)이 주어진다.
출력
첫째 줄에 새로운 수의 자릿수를 출력한다.
import java.util.Scanner;
public class baekjoon1748 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int length = length(n);
int res = (int) (n - Math.pow(10, length - 1) + 1) * length;
length--;
for (int i = length; i >= 0; i--) {
res += 9 * Math.pow(10, i - 1) * i;
}
System.out.println(res);
}
static int length(int n) {
int len = 0;
while (n != 0) {
n /= 10;
len++;
}
return len;
}
}