๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[SW Expert Academy] - (D3)5515. 2016๋…„ ์š”์ผ ๋งž์ถ”๊ธฐ

by ์ฃผ๋ฐœ2 2020. 5. 27.
๋ฐ˜์‘ํ˜•

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWWOwecaFrIDFAV4&categoryId=AWWOwecaFrIDFAV4&categoryType=CODE

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

์ฝ”๋“œ

import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int T = scan.nextInt();
		for(int tc=1; tc<=T; tc++) {
			
			// 2016๋…„ m์›” d์ผ, 2016๋…„ 1์›” 1์ผ = ๊ธˆ์š”์ผ, ์œค๋…„(2์›”=29์ผ)
			// ์›”์š”์ผ = 0, ํ™” = 1, ์ˆ˜ = 2, ..., ์ผ์š”์ผ = 6 ์ถœ๋ ฅ
			int m = scan.nextInt();	
			int d = scan.nextInt();
			int allDays = 0;
			
			int[] months = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
			
			for(int i=0; i<m-1; i++) {
				allDays += months[i];	// ํ•ด๋‹น ์›” ๋”ํ•ด์คŒ.
			}
			
			allDays += (d-1) + 4;	// ์ผ ์ˆ˜ ์ฐจ์ด๋งŒํผ ๋”ํ•˜๊ณ , 1์›” 1์ผ์ด ๊ธˆ์š”์ผ(4)์ด๋ฏ€๋กœ 4๋ฅผ ๋”ํ•ด์คŒ..
			System.out.println("#" + tc + " " + allDays%7);
		}
		
		scan.close();
	}

}

ํ’€์ด

ํ’€์ด๋Š” ์›”, ์ผ ์ฐจ์ด๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๊ณ  4๋ฅผ ๋”ํ•ด์„œ 7๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค...

์ž…๋ ฅ๋ฐ›์€ ์›”(m) - 1์›”(1) ๋งŒํผ months ๋ฐฐ์—ด์˜ ๊ฐ’๋“ค์„ ๋ชจ๋‘ ๋”ํ•ด์ค€๋‹ค...

๋งŒ์•ฝ m = 4๋ผ๋ฉด ... 1์›” 2์›” 3์›”์˜ ์ผ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์ค€๋‹ค

for ๋ฌธ์—์„œ m-1์€ 3์ด๋ฏ€๋กœ, months์˜ 0, 1, 2 ๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฏ€๋กœ 31 + 29 + 31์ด ๋œ๋‹ค.

์ด๋Ÿฐ์‹์œผ๋กœ ์ฐจ์ด๋‚˜๋Š” ์›”๋งŒํผ ์ผ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ผ์ˆ˜๋Š” ์ž…๋ ฅ๋ฐ›์€ ์ผ(d) - 1 ์„ ๋”ํ•˜๊ณ  +4๋ฅผ ๋”ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ 4๋Š” 1์›” 1์ผ์ด ๊ธˆ์š”์ผ(4) ์ด๋ฏ€๋กœ, 4๋ฅผ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ ํ›„ 7๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋!

1์›”์˜ ๋‹ฌ๋ ฅ์„ ๋ชจ๋‘ ์จ์„œ ๊ทœ์น™์„ ์ฐพ์•„์„œ ํ’€์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€