Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.04.11 - [BOJ] 1748. 수 이어쓰기 1 #52

Closed
suhyunsim opened this issue Apr 11, 2021 · 0 comments
Closed

21.04.11 - [BOJ] 1748. 수 이어쓰기 1 #52

suhyunsim opened this issue Apr 11, 2021 · 0 comments
Assignees
Labels
브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Apr 11, 2021

문제

핵심 아이디어

  • N이 너무 커서 N개의 수를 자릿수 별로 나눠서 문제 해결

어려운 점, 실수

풀이

  • 내가 푼 풀이
package main.java.com.poogle.BOJ.Q1748;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = String.valueOf(n).length();
        int ans = 0;
        if (k > 1) {
            for (int i = 1; i < k; i++) {
                ans += i * (Math.pow(10, i) - Math.pow(10, i - 1));
            }
            ans += k * (n - (Math.pow(10, k - 1) - 1));
        } else {
            ans = n;
        }
        System.out.println(ans);
    }
}
  • 참고
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long ans = 0;
        for (int start=1, len=1; start<=n; start*=10, len++) {
            int end = start*10-1;
            if (end > n) {
                end = n;
            }
            ans += (long)(end - start + 1) * len;
        }
        System.out.println(ans);
    }
}
@suhyunsim suhyunsim added 성공 맞은 문제 브루트포스 완전탐색 실버 BOJ - 실버 labels Apr 11, 2021
@suhyunsim suhyunsim added this to the 4월 2주 차 milestone Apr 11, 2021
@suhyunsim suhyunsim self-assigned this Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant