본문 바로가기

알고리즘/코드 연습

[백준 - 1924] 2007년

반응형

1/1 월요일이다

 

month월 day일은 무슨요일인지를 출력하라.

 

입력 

1 1

출력

MON

 

요일은 7일마다 반복된다.

ex) 1/1 월요일, 1/8 월요일

 

hint.

1/18일은 총 일수를 따지면 18일이고 18%7 = 4

1/8 월요일  8%7 = 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include <string>
 
int findFirstDay(int M) {
    int days = 0;
    
    for (int month = 0; month < M ; month++) {
 
        if (month == 2) days += 28;
        else if (month == 4 || month == 6 || month == 9 || month == 11) days += 30;
        else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) days += 31;
    }
 
    return days;
}
 
int main() {
    const std::vector<std::string> week = { "MON""TUE""WED""THU""FRI""SAT""SUN" };
 
    int month, day;
 
    std::cin >> month >> day;
 
    int flag = (findFirstDay(month) + day) % 7 - 1;
    if (flag < 0) flag += 7;
 
    std::cout << week[flag];
 
}
cs

 

 

 

 

조금더 간단히 짜본 코드이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
 
    const int a[] = { 0312831303130313130313031 };
    const std::vector<std::string> week = { "SUN""MON""TUE""WED""THU""FRI""SAT" };
 
    int month, day;
    std::cin >> month >> day;
 
    int days = 0;
    for (int i = 0; i < month; i++) {
        days += a[i];
    }
    std::cout<<week[(days + day)%7];
    
}
cs

 

 

출처 : https://www.acmicpc.net/problemset

반응형

'알고리즘 > 코드 연습' 카테고리의 다른 글

[백준 - 1316] 그룹 단어 체커  (0) 2020.11.27
[백준-1764] 듣보잡  (0) 2020.07.31
[백준 - 2798] 블랙잭  (0) 2020.07.24
[LeetCode] Roman to Integer  (0) 2020.07.21
[LeetCode] Palindrome Number  (0) 2020.07.20