๊ธฐ๋ก๋ฐฉ

BOJ_1271 : ์—„์ฒญ๋‚œ ๋ถ€์ž2 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1271 : ์—„์ฒญ๋‚œ ๋ถ€์ž2

Soom_1n 2022. 10. 4. 14:56

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1271๋ฒˆ: ์—„์ฒญ๋‚œ ๋ถ€์ž2

์ฒซ์งธ ์ค„์—๋Š” ์ตœ๋ฐฑ์ค€ ์กฐ๊ต๊ฐ€ ๊ฐ€์ง„ ๋ˆ n๊ณผ ๋ˆ์„ ๋ฐ›์œผ๋Ÿฌ ์˜จ ์ƒ๋ช…์ฒด์˜ ์ˆ˜ m์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ m ≤ n ≤ 101000, m๊ณผ n์€ 10์ง„์ˆ˜ ์ •์ˆ˜)

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ 10์˜ 1000์Šน์ด๋ฏ€๋กœ '0'์ด ์ฒœ๊ฐœ์ด๋ฏ€๋กœ ์—„์ฒญ ํฐ ์ˆ˜์ด๋‹ค. 0์ด 64๊ฐœ๋งŒ ๋˜์–ด๋„ '๋ฌด๋Ÿ‰๋Œ€์ˆ˜'๋ผ๊ณ  ํ•œ๋‹ค.
    • int์™€ long๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๋Š” ํฐ ์ˆ˜์ด๋‹ค. (long์€ 19์ž๋ฆฌ)
Type ๋ฒ”์œ„
int -2,147,483,648 ~ 2,147,483,647
long -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        BigInteger n = new BigInteger(st.nextToken());
        BigInteger m = new BigInteger(st.nextToken());

        System.out.println(n.divide(m));
        System.out.println(n.remainder(m));
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ํฐ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด math๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ BigIntegerํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    • ์ธ์Šคํ„ด์Šค ์ธ์ˆ˜๋กœ ์ž…๋ ฅ๋œ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ๋„˜๊ธฐ๋ฉด ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.
    • ์ •์˜๋œ ์‚ฌ์น™์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ณ„์‚ฐํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ธŒ๋ก ์ฆˆ 5๋ฌธ์ œ์˜€์ง€๋งŒ, ํฐ ์ˆ˜ ์—ฐ์‚ฐ์ด์–ด์„œ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์ด ํ•„์ˆ˜์ด๋ฏ€๋กœ ์˜ค๋‹ต๋ฅ ์ด ์•„์ฃผ ๋†’๋‹ค.
  • ํ•„์ž๋„ C++๊ณต๋ถ€ํ• ๋•Œ ๊ฒฐ๊ตญ ํ’€์ง€๋ชปํ•˜๊ณ  ๋„˜์–ด๊ฐ”๋‹ค๊ฐ€, java๋กœ ๋‹ค์‹œ ํ’€๋ฉด์„œ ๋‹ต์„ ๋ณด๊ณ  ์ฐธ๊ณ ํ•ด ํ’€์—ˆ๋‹ค.

728x90