Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java] ๋ฐฑ์ค€ 1008๋ฒˆ ๋ฌธ์ œ ํ’€์ด - ์ˆซ์ž ์ž๋ฃŒํ˜• int, long, float, double / / ๋‚˜๋ˆ—์…ˆ ์†Œ์ˆ˜์ ์ดํ•˜ ์ถœ๋ ฅ / ์ •์ˆ˜<->์‹ค์ˆ˜ ํ˜•๋ณ€ํ™˜ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[Java] ๋ฐฑ์ค€ 1008๋ฒˆ ๋ฌธ์ œ ํ’€์ด - ์ˆซ์ž ์ž๋ฃŒํ˜• int, long, float, double / / ๋‚˜๋ˆ—์…ˆ ์†Œ์ˆ˜์ ์ดํ•˜ ์ถœ๋ ฅ / ์ •์ˆ˜<->์‹ค์ˆ˜ ํ˜•๋ณ€ํ™˜

๋น„๋น„ bibi 2020. 11. 6. 11:52

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 1008๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ณผ์ •์˜ ๊ธฐ๋ก์ž…๋‹ˆ๋‹ค.

www.acmicpc.net/problem/1008

 

1008๋ฒˆ: A/B

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A/B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net

 

๋ฌธ์ œ

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A/B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— A/B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์‹ค์ œ ์ •๋‹ต๊ณผ ์ถœ๋ ฅ๊ฐ’์˜ ์ ˆ๋Œ€์˜ค์ฐจ ๋˜๋Š” ์ƒ๋Œ€์˜ค์ฐจ๊ฐ€ 10-9 ์ดํ•˜์ด๋ฉด ์ •๋‹ต์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 1 3 ์„ ์ž…๋ ฅํ•˜๋ฉด 0.33333333333333333333333333333333 ์„ ์ถœ๋ ฅ.

 


1000๋ฒˆ ๋ฌธ์ œ์™€ ๋˜‘๊ฐ™์ด ๋งŒ๋“ค๊ณ  ์—ฐ์‚ฐ์ž๋งŒ /๋กœ ๋ฐ”๊พธ๋ฉด ๋˜๊ฒ ๋„ค~ ํ–ˆ๋Š”๋ฐ ์–ด๋ฆผ์—†๋Š” ์†Œ๋ฆฌ์˜€๋‹ค.

โ— ๋‘ int ๋ณ€์ˆ˜๋ฅผ ๋‚˜๋ˆ„๋ฉด ๊ฒฐ๊ณผ๊ฐ’์€ ํ•ญ์ƒ int๋กœ ์ถœ๋ ฅ๋œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด 1 / 3 = 0.3333... ์ด ๋‚˜์™€์•ผ ํ•˜๋Š”๋ฐ 0์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.
์ •์ˆ˜ํ˜•(int)์€ ์†Œ์ˆ˜์  ์•„๋ž˜ ์ž๋ฆฌ๋Š” ํ•ญ์ƒ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
๐Ÿ‘‰ ๋‚˜๋ˆ—์…ˆ์„ ํ†ตํ•ด ์†Œ์ˆ˜์  ์ดํ•˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด,
๋‘ ์ˆ˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ์‹ค์ˆ˜ํ˜•(float ๋˜๋Š” double)์ด์–ด์•ผ ํ•œ๋‹ค.
์ฐธ๊ณ  : mwultong.blogspot.com/2007/01/java-int-long-divide.html. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์—์„œ "๋‘ ์ •์ˆ˜ A์™€ B"๋ฅผ ๋ฐ›์œผ๋ผ๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ
A์™€ B๋ฅผ int๋กœ ๋ฐ›๋˜, ์ •์ˆ˜๋ฅผ ์‹ค์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด์„œ ์—ฐ์‚ฐํ•œ ๋’ค ์ถœ๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

๐Ÿ‘‰ ์ •์ˆ˜-> ์‹ค์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜ (casting)
(int-> double) : intํ˜•์˜ ๋ณ€์ˆ˜๋ช… ์•ž์— (double)์„ ๋ถ™์ด๋ฉด doubleํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜์ด ๋œ๋‹ค.
(int -> float) : intํ˜•์˜ ๋ณ€์ˆ˜๋ช… ์•ž์— (float)๋ฅผ ๋ถ™์ด๋ฉด floatํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜์ด ๋œ๋‹ค.

โ€ป ๊ทธ์™ธ ์ž๋ฐ” ํ˜•๋ณ€ํ™˜ ์ •๋ฆฌ : coding-factory.tistory.com/130 ์ฐธ๊ณ . ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

โœ” ์ด์ฏค์—์„œ ์ž๋ฐ” ์ˆซ์ž ์ž๋ฃŒํ˜• ๊ฐœ๋…์ •๋ฆฌ

์ฐธ๊ณ  : wikidocs.net/204 ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
(์ž์ฃผ ์“ฐ์ด๋Š” ๊ฒƒ๋งŒ ์ •๋ฆฌ. //์ดํ•˜๋Š” ๊ฑฐ์˜ ์“ฐ์ด์ง€ ์•Š์Œ)

  • ์ •์ˆ˜ : int, long // byte, short ๋“ฑ
    • int๋Š” -2147483648 ~ 2147483647 ๊นŒ์ง€ ํ‘œํ˜„ ๊ฐ€๋Šฅ.
    • long์€ -9223372036854775808 ~ 9223372036854775807 ๊นŒ์ง€ ํ‘œํ˜„ ๊ฐ€๋Šฅ.
    • ์‚ฌ์šฉ )
      int age = 10;
      long countOfStar = 8764827384923849L;
    • long๋ณ€์ˆ˜์— ๊ฐ’์„ ๋Œ€์ž…ํ•  ๋•Œ, ๋Œ€์ž…ํ•˜๋Š” ๊ฐ’์ด 2147483647๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์ ‘๋ฏธ์‚ฌ L์„ ๋ถ™์—ฌ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
      ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ์‹ค์ˆ˜ : float, double
    • float๋ณด๋‹ค double์ด ๋” ๋„“์€ ๋ฒ”์œ„์˜ ์‹ค์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์‚ฌ์šฉ )
      float PI = 3.14f; ๋˜๋Š” 3.14F; ๋˜๋Š” (float)3.14;
      double longPI =
      3.14159265358979323846;
    • float๋ณ€์ˆ˜์— ๊ฐ’์„ ๋Œ€์ž…ํ•  ๋•Œ๋Š” ์ ‘๋ฏธ์‚ฌ f ๋˜๋Š” F๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜, ๊ฐ’ ์•ž์— (float)๋ฅผ ์ ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
      ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

๋‚ด ์ฝ”๋“œ

import java.util.Scanner;

public class BJ_1008 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int numA = s.nextInt();
        int numB = s.nextInt();

        if (numA <= 0 || numB >= 10 ) return;

        double D_numA = (double)numA;
        double D_numB = (double)numB;

        System.out.println(D_numA / D_numB);
    }
}