๊ธฐ๋ก๋ฐฉ

BOJ_28470 : ์Šฅ~๋นก! ๋นก~์Šฅ! ๋ณธ๋ฌธ

CodingTest/Java

BOJ_28470 : ์Šฅ~๋นก! ๋นก~์Šฅ!

Soom_1n 2024. 11. 26. 11:15

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



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

  • ๊ณต๊ฒฉ์„ ๋จผ์ €ํ–ˆ์„ ๋•Œ์™€ ํšŒํ”ผ๋ฅผ ๋จผ์ € ํ–ˆ์„ ๋•Œ ์ค‘์—์„œ ์•„๋“œ๋ ˆ๋‚ ๋ฆฐ์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • Ki๊ฐ€ 1๋ณด๋‹ค ํฌ๋ฉด Ai์— ๊ณฑํ•˜๊ณ , 1์ดํ•˜๋ฉด Bi์— ๊ณฑํ•˜๋ฉด ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Ki๊ฐ€ ์†Œ์ˆ˜์   ์•„๋ž˜ ์ฒซ์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์†Œ์ˆซ์  ๊ณ„์‚ฐ์— ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค.

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

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int N = Integer.parseInt(br.readLine());
        StringTokenizer st1 = new StringTokenizer(br.readLine());
        StringTokenizer st2 = new StringTokenizer(br.readLine());
        StringTokenizer st3 = new StringTokenizer(br.readLine());

        long sum = 0;

        for (int i = 0; i < N; i++) {
            int A = Integer.parseInt(st1.nextToken());
            int B = Integer.parseInt(st2.nextToken());
            float K = Float.parseFloat(st3.nextToken());

            if (K > 1) {
                sum += (int) (A * (K * 10) / 10) - B;
            } else {
                sum += A - (int) (B * (K * 10) / 10);
            }
        }

        bw.write(Long.toString(sum));
        bw.flush();
    }
}

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

  • Ki์˜ ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ์งธ ์ž๋ฆฌ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด, 10์„ ๊ณฑํ•œ ํ›„ 10์„ ๋‚˜๋ˆ ์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.
  • Ai * Ki ํ˜น์€ Bi * Ki์˜ ๋‚ด๋ฆผ์€ (int) ํ˜• ๋ณ€ํ™˜์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์˜ค๋‹ต๋ฅ ์ด ๋†’๊ธธ๋ž˜ ์™œ๊ทธ๋Ÿฐ๊ฐ€ ํ–ˆ๋”๋‹ˆ ๋ถ€๋™์†Œ์ˆ˜์ ์˜ ์˜ค์ฐจ๋ฅผ ๊น๊นํžˆ ๋ณด๋Š” ๋ฌธ์ œ์˜€๋‹ค.
  • doubleํ˜•์œผ๋กœ ๊ณฑ์…ˆ ์ดํ›„ ๋‚ด๋ฆผ์„ ์ˆ˜ํ–‰ํ•˜๋”๋ผ๋„ ์˜ค์ฐจ๊ฐ€ ์ƒ๊ธฐ๋ฏ€๋กœ, ๊ผญ 10์„ ๊ณฑํ•˜๊ณ  10์„ ๋‚˜๋ˆ„๋Š” ์ž‘์—…์„ ํ†ตํ•ด ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ์งธ ์ž๋ฆฌ๋ฅผ ์ •ํ™•ํžˆ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์ค‘์š”ํ–ˆ๋‹ค.
728x90