๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S5] ๋ฐฑ์ค€ 1340๋ฒˆ ์—ฐ๋„ ์ง„ํ–‰๋ฐ”

by immgga 2024. 8. 8.

์ถœ์ฒ˜: unsplash.com

 

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

Silver 5

#๊ตฌํ˜„ #๋ฌธ์ž์—ด #ํŒŒ์‹ฑ

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

๋‚ ์งœ๊ฐ€ ๋ถ„๊นŒ์ง€ ์ž…๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ง„ํ–‰๋„์˜ ๋‹จ์œ„๋ฅผ ํ•˜๋ฃจ(365)๋กœ ์ •ํ•ด์„œ ์ง„ํ–‰๋„๋ฅผ ๊ตฌํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

ํ•˜๋ฃจ์—์„œ ๋” ์ชผ๊ฐœ์„œ ๋ถ„์œผ๋กœ ์ •ํ•ด์„œ ์ง„ํ–‰๋„๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. ํ•˜๋ฃจ๋ฅผ ๋ถ„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 1440๋ถ„์ด๋‹ค.

 

๋ฐฑ๋ถ„์œจ์„ ๊ตฌํ•˜๋Š” ๊ณต์‹์€ ์‰ฝ๋‹ค.

์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ๊ฐ€ 1์›” 1์ผ์—์„œ๋ถ€ํ„ฐ ์–ผ๋งˆ๋‚˜ ์ง€๋‚œ ๋‚ ์งœ์ธ์ง€ ๊ตฌํ•œ ๋‹ค์Œ์—, hour๋ฅผ 60์„ ๊ณฑํ•ด ๋”ํ•ด์ฃผ๊ณ , ๋ถ„์„ ๋”ํ•ด์ฃผ๋ฉด 1๋…„์—์„œ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ์–ผ๋งˆ๋‚˜ ์ง€๋‚ฌ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ๊ตฌํ•ด 365 ๋˜๋Š” 366์œผ๋กœ ๋‚˜๋ˆ ์„œ 100์„ ๊ณฑํ•˜๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค.

365 ๋ง๊ณ  366์„ ์“ฐ๋Š” ์ด์œ ๋Š” ์œค๋…„์„ ์˜์‹ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. ์œค๋…„์€ 2์›”์ด 29์ผ๊นŒ์ง€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋ฃจ๊ฐ€ ๋” ๊ธธ๋‹ค.

1์›” 1์ผ๋กœ๋ถ€ํ„ฐ ์–ผ๋งˆ๋‚˜ ์‹œ๊ฐ„์ด ์ง€๋‚ฌ๋Š”์ง€ ๋ถ„์œผ๋กœ ํ™˜์‚ฐํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

val dayProgressNum = (localDateTime.dayOfYear - 1) * 1440.0 + hour * 60 + minute

localDateTime์˜ dayOfYear๋ฅผ ์ด์šฉํ•ด 365์ผ ์ค‘์—์„œ ์–ผ๋งˆ๋‚˜ ๋‚ ์งœ๊ฐ€ ์ง€๋‚ฌ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. -1์„ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์œ„ ๊ฐ’์ด 1์›” 1์ผ์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ง„ํ–‰๋„์—์„œ๋Š” 1์›” 1์ผ์„ ํฌํ•จํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader
import java.time.LocalDateTime

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val monthParser = mapOf(
        "January" to 1, "February" to 2, "March" to 3, "April" to 4,
        "May" to 5, "June" to 6, "July" to 7, "August" to 8,
        "September" to 9, "October" to 10, "November" to 11, "December" to 12,
    )
    val dateString = readLine().split(" ")

    val month = monthParser[dateString.first()]!!
    val dayOfMonth = dateString[1].replace(",", "").toInt()
    val year = dateString[2].toInt()

    val time = dateString[3].split(":")
    val hour = time.first().toInt()
    val minute = time.last().toInt()

    val localDateTime = LocalDateTime.of(year, month, dayOfMonth, hour, minute, 0)
    val dayProgressNum = (localDateTime.dayOfYear - 1) * 1440.0 + hour * 60 + minute

    // ์œค๋…„์ด๋ฉด 366์ผ
    val yearToMinute = if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) 1440 * 366
    else 1440 * 365
    println(dayProgressNum / yearToMinute * 100)

}

 

๋ฌธ์ œ ํ’€์ด

์›”์ด ํ’€ ์˜์–ด๋กœ ์ž…๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์šฉ parser map์„ ๋งŒ๋“ค์–ด์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ , dateString์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ LocalDateTime์œผ๋กœ ๋ฐ”๊ฟ” ์ฃผ์—ˆ๋‹ค.

 

LocalDateTime ์ „์šฉ ๋ณ€์ˆ˜์ธ dayOfYear๋กœ 365์ผ ์ค‘ ๋ฉฐ์น ์ด ์ง€๋‚ฌ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  hour์™€ minute๋ฅผ ๋”ํ•ด ์ง€๋‚œ ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ๋ณ€ํ™˜ํ•ด ๊ตฌํ•ด ์ฃผ์—ˆ๋‹ค.

์ด์ œ 365์ผ๋กœ ๋‚˜๋ˆ ์ฃผ๊ณ  100์„ ๊ณฑํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์œค๋…„์ธ ๊ฒฝ์šฐ์—๋Š” 2์›” 29์ผ๊นŒ์ง€ ์žˆ์–ด์„œ ํ•˜๋ฃจ๊ฐ€ ๋” ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 366์ผ๋กœ ์„ค์ •ํ•ด์„œ ๊ณ„์‚ฐํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

์ง„ํ–‰๋„๋ฅผ ๋ถ„์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์–ด๋ ต์ง€ ์•Š์€ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

728x90