백준 1748번 수 이어 쓰기 1 JAVA

YB·2025년 12월 21일

링크텍스트

설명

시간복잡도: O(logN), 공간복잡도: O(1)

회독

  • [ x ] 1회
  • 2회
  • 3회

코드

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        if(n<10){
            System.out.println(n);
            return;
        }

        long count = 0;
        int base = 1; //해당 자릿수 시작값
        int digit = 1;

        while(base*10<=n){
            count+=(long)(base*9)*digit;
            base*=10;
            digit++;
        }

        // 마지막 자릿수 구간
        count+=(long)(n-base+1)*digit;

        System.out.println(count);
    }
}

profile
안녕하세요

0개의 댓글