[Codetree] 그 요일은
29 Sep 2026
Reading time ~2 minutes
해당 문제는 코드트리 그 요일은에서 풀어보실 수 있습니다.
풀이1 구현
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m1 = sc.nextInt();
int d1 = sc.nextInt();
int m2 = sc.nextInt();
int d2 = sc.nextInt();
String A = sc.next();
// Please write your code here.
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
int[] month = {0,31,29,31,30,31,30,31,31,30,31,30,31};
String[] day = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int ans = 0;
int cnt = 0;
while(true) {
if(day[cnt%7].equals(A)) ans++;
if(m1 == m2 && d1 == d2) break;
cnt++;
d1++;
if(d1>month[m1]){
m1++;
d1=1;
}
}
System.out.println(ans);
}
}
풀이2 구현
import java.util.Scanner;
public class Main {
public static int numOfDays(int m, int d) {
// 계산 편의를 위해 월마다 몇 일이 있는지를 적어줍니다.
int[] days = new int[]{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalDays = 0;
// 1월부터 (m - 1)월 까지는 전부 꽉 채워져 있습니다.
for(int i = 1; i < m; i++)
totalDays += days[i];
// m월의 경우에는 정확이 d일만 있습니다.
totalDays += d;
return totalDays;
}
public static int numOfDay(String s) {
// 간단한 비교를 위해 요일을 숫자로 나타내줍니다.
if(s.equals("Mon")) return 0;
else if(s.equals("Tue")) return 1;
else if(s.equals("Wed")) return 2;
else if(s.equals("Thu")) return 3;
else if(s.equals("Fri")) return 4;
else if(s.equals("Sat")) return 5;
return 6;
}
public static int ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 변수 선언 및 입력
int m1 = sc.nextInt();
int d1 = sc.nextInt();
int m2 = sc.nextInt();
int d2 = sc.nextInt();
String A = sc.next();
int startDate = numOfDays(m1, d1);
int endDate = numOfDays(m2, d2);
int curDay = numOfDay("Mon");
for(int date = startDate; date <= endDate; date++) {
// 오늘의 요일이 A요일과 같다면 정답에 추가합니다.
if(curDay == numOfDay(A)) ans++;
curDay = (curDay + 1) % 7;
}
// 출력
System.out.print(ans);
}
}