๊ธฐ๋ก๋ฐฉ

BOJ_1340 : ์—ฐ๋„ ์ง„ํ–‰๋ฐ” ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1340 : ์—ฐ๋„ ์ง„ํ–‰๋ฐ”

Soom_1n 2022. 9. 20. 15:01

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

 

1340๋ฒˆ: ์—ฐ๋„ ์ง„ํ–‰๋ฐ”

ํ‰๋…„์ผ ๋•Œ, ๊ฐ ๋‹ฌ์€ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31์ผ์ด ์žˆ๋‹ค. ์œค๋…„์—๋Š” 2์›”์ด 29์ผ์ด๋‹ค. ์œค๋…„์€ ๊ทธ ํ•ด๊ฐ€ 400์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ํ•ด ์ด๊ฑฐ๋‚˜, 4๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด์„œ, 100์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€

www.acmicpc.net



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

  • ์ž…๋ ฅ ๋ฐ›์€ ์‹œ๊ฐ„์ด ํ•ด๋‹น ๋…„๋„์—์„œ ๋ช‡ ํผ์„ผํŠธ ์ง„ํ–‰ ๋œ ๊ฑด์ง€ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋˜๋ฏ€๋กœ ํ˜•์‹์— ๋งž๊ฒŒ ๋ฐ›์•„์•ผํ•œ๋‹ค.
  • ์œค๋…„์„ ํ™•์ธํ•ด์•ผํ•œ๋‹ค.

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

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input[] = sc.nextLine().split(",| |:");
        String m = "January, February, March, April, May, June, July, August, September, October, November, December";
        int month = Arrays.asList(m.split(", ")).indexOf(input[0]);
        int day = Integer.parseInt(input[1]);
        int year =Integer.parseInt(input[3]);
        int hour = Integer.parseInt(input[4]);
        int minute = Integer.parseInt(input[5]);

        int m_list[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        double now = day - 1; // sum day
        double full = 365;

        boolean leap = false;
        if (year % 400 == 0 || (year%4 == 0 && year%100 != 0)){
            leap = true;
            full++;
        }
        for (int i = 0; i < month; i++) {// sum month
            if (i == 1 && leap)
                now += 29;
            else
                now += m_list[i];
        }

        now = now*24*60 + hour*60 + minute;
        full = full*24*60;
        System.out.println(now/full*100);
    }
}

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

  • ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋ฐ›์€ ํ›„ 3๊ฐ€์ง€ ๊ธฐ์ค€์œผ๋กœ split()ํ•ด String๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  • month๋Š” ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์˜๋‹จ์–ด๋ฅผ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ›„ split(), Arrays.asList().indexOf()๋กœ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • now๋Š” ์ž…๋ ฅ๋ฐ›์€ ์‹œ๊ฐ„์„ ๋ถ„ ๋‹จ์œ„๋กœ ํ™˜์‚ฐํ•˜๋Š”๋ฐ, ํ˜„์žฌ ๋‚ ์งœ๋Š” ์•„์ง ์ง€๋‚˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ -1 ํ›„ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • full์€ ํ•ด๋‹น ๋…„๋„์˜ ์ „์ฒด ๋ถ„ ํ™˜์‚ฐ ๊ฐ’์ด๋‹ค.
  • ๋ฐฑ๋ถ„์œจ์„ ์ถœ๋ ฅํ•œ๋‹ค

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ ์œค๋…„์„ ์ ์šฉํ•˜๋Š”๊ฒŒ ๊ผฌ์—ฌ์„œ ๊ณ ์น  ๋ถ€๋ถ„์ด ๋งŽ์•˜๋‹ค.
  • ์ฝ”๋“œ๋ฅผ ์ข€๋” ๊ตฌ๋ถ„ํ•˜๋ฉฐ ์งœ์•ผ๋  ๊ฒƒ ๊ฐ™๋‹ค. java๋Š” ์•„์ง ์ฃผ์„์„ ๋งŽ์ด๋‹ฌ์•„์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1380 : ๊ท€๊ฑธ์ด  (0) 2022.09.22
BOJ_1343 : ํด๋ฆฌ์˜ค๋ฏธ๋…ธ  (0) 2022.09.21
BOJ_1331 : ๋‚˜์ดํŠธ ํˆฌ์–ด  (0) 2022.09.19
BOJ_1312 : ์†Œ์ˆ˜  (0) 2022.09.18
BOJ_1094 : ๋ง‰๋Œ€๊ธฐ  (0) 2022.09.18