자바 백준 1924번
브론즈 1
https://www.acmicpc.net/problem/1924
1924번: 2007년
첫째 줄에 빈 칸을 사이에 두고 x(1 ≤ x ≤ 12)와 y(1 ≤ y ≤ 31)이 주어진다. 참고로 2007년에는 1, 3, 5, 7, 8, 10, 12월은 31일까지, 4, 6, 9, 11월은 30일까지, 2월은 28일까지 있다.
www.acmicpc.net
문제 보기
분류: 수학, 구현
문제 풀기
Zeller's congruence 를 사용하여 해당 요일을 찾을 수 있다.
1월 또는 2월의 경우 연은 1 을 빼고 월은 12 를 더하여 식에 대입한다.
0 부터 6 까지 숫자로 표현된 요일을 해당 문자로 변환하여 출력한다.
코드 보기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = 2007;
int x = sc.nextInt(); // x월
int y = sc.nextInt(); // y일
String result = findWhatDayOfTheWeek(year, x, y);
System.out.println(result); // 요일 출력
}
public static String findWhatDayOfTheWeek(int year, int x, int y) {
// 1월과 2월은 이전 연도의 13월과 14월로 간주
if (x == 1 || x == 2) {
x += 12;
year -= 1;
}
// Zeller's Congruence를 사용하여 요일 계산
int z = (y + (13 * (x + 1) / 5) + year + (year / 4) - (year / 100) + (year / 400)) % 7;
// 요일을 숫자에서 문자열로 변환
String dayOfWeek = "";
switch (z) {
case 0:
dayOfWeek = "SAT";
break;
case 1:
dayOfWeek = "SUN";
break;
case 2:
dayOfWeek = "MON";
break;
case 3:
dayOfWeek = "TUE";
break;
case 4:
dayOfWeek = "WED";
break;
case 5:
dayOfWeek = "THU";
break;
case 6:
dayOfWeek = "FRI";
break;
}
return dayOfWeek;
}
}
참고 하기
https://en.wikipedia.org/wiki/Zeller%27s_congruence#Common_simplification
Zeller's congruence - Wikipedia
From Wikipedia, the free encyclopedia Algorithm to calculate the day of the week Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be consi
en.wikipedia.org
'공부하기 > 백준' 카테고리의 다른 글
[Java] 백준 풀기 10952 - A+B 5 (0) | 2023.06.10 |
---|---|
[Java] 백준 풀기 17413 - 단어 뒤집기 2 (0) | 2023.06.09 |
[Java] 백준 풀기 10988 - 팰린드롬인지 확인하기 (1) | 2023.06.07 |
[Java] 백준 풀기 1463 - 1로 만들기 (0) | 2023.06.06 |
[Java] 백준 풀기 4153 - 직각삼각형 (0) | 2023.06.05 |