본문 바로가기
Algorithm

[백준] 1790

by ikii 2023. 8. 29.

코드 1, 성공, 230816

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");

        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int len = 0;

        for (int i = 1; i < str[0].length(); i++) {
            len += 9 * Math.pow(10, i - 1) * i;
        }

        len += ((n - Math.pow(10, str[0].length() - 1)) + 1) * str[0].length();

        if (m > len) {
            System.out.println(-1);
        } else {
            if (m < 10) {
                System.out.println(m);
            } else {
                int i = 1;
                while (true) {
                    if ((9 * Math.pow(10, i - 1)) * (i) > m) {
                        break;
                    } else {
                        m -= (9 * Math.pow(10, i - 1)) * (i);
                    }
                    i += 1;
                }

                int a;
                int b;

                if(m % i == 0){
                    a = m/i - 1;
                    String result = Integer.toString((int) Math.pow(10, i - 1) + a);
                    System.out.println(result.charAt(result.length()-1));
                } else {
                    a = m/i;
                    b = m % i;
                    String result = Integer.toString((int) Math.pow(10, i - 1) + a);
                    System.out.println(result.charAt(b-1));
                }
            }
        }
    }
}

전체 입력 받음

총 가능한 길이 구하기
(n 자릿수 - 1)까지 가능한 총 길이 len에 저장
n - n의 자리수 시작하는수(2자리면 10, 3자리면 100) * n 자릿수

총 길이 < m인 경우

  • -1 출력
    아닌 경우
  • m이 한 자리 수인 경우 그대로 출력
  • 그 외 - 자릿수 시작 기준으로 어느 구간에 위치하는지 찾음
    • m이 0이 되지 않는 선에서 순차적으로 자릿수 별로 최대 글자 길이 수 빼줌
    • 만약 m % 자릿수(i) != 0
      • m/i의 b-1(java 문법 상은 0부터 시작인데 숫자 체크할 땐 1부터 시작했기에) index
    • 전 글자 + 나머지
      • m/i -1 (전 숫자)의 마지막 글자

자릿수 계산 참고
자릿수 1(19) : 9 * 1
자릿수 2(10
99) : 90 * 2 => 180
자릿수 3(100~999) : 900 * 3 => 2700
4 : 9000 * 4
5 : 90000 * 5


코드 2, 성공, 230816

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");

        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int len = 0;

        for (int i = 1; i < str[0].length(); i++) {
            len += 9 * Math.pow(10, i - 1) * i;
        }

        len += ((n - Math.pow(10, str[0].length() - 1)) + 1) * str[0].length();

        if (m > len) {
            System.out.println(-1);
        } else {
            if (m < 10) {
                System.out.println(m);
            } else {
                int i = 1;

                while(true){
                    if (m - (9 * Math.pow(10, i - 1)) * (i) < 0){
                        break;
                    } else {
                        m -= (9 * Math.pow(10, i - 1)) * (i);
                    }
                    i += 1;
                }

                if(m % i == 0){
                    String result = Integer.toString((int) Math.pow(10, i - 1) + (m/i - 1));
                    System.out.println(result.charAt(result.length()-1));
                } else {
                    String result = Integer.toString((int) Math.pow(10, i - 1) + (m/i));
                    System.out.println(result.charAt((m%i)-1));
                }
            }
        }
    }
}

코드 1보다 직관적으로 m이 0보다 작아지지 않은 경우만 빼다가 m%i == 0이면 i-1번째 수의 마지막 index를 가리키고 m%i != 0이면 i번째의 m%i - 1 index를 가리킨다

'Algorithm' 카테고리의 다른 글

[백준] 5430  (0) 2023.08.29
[백준] 16927  (0) 2023.08.29
[백준] 22238  (0) 2023.08.29
[백준] 28291  (0) 2023.08.29
[백준] 14503  (0) 2023.08.11

댓글