C++ Algorithm & Study/C++ & Algorithm Strategies
[Programmers] 2016년
GameChoi
2023. 2. 1. 19:42
https://school.programmers.co.kr/learn/courses/30/lessons/12901
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
0. Headers
#include <string>
#include <vector>
using namespace std;
1. 알고리즘
- 1월 1일이 금요일이고 윤달이므로 날짜가 아래처럼 나옴
string solution(int a, int b) {
string answer = "";
vector<string> day {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
vector<int> month {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
- 우선 b일을 생성한 후 a월까지 반복문을 돌려서 a월을 더함
- 더한 값을 7로 나눈 나머지를 day에 대입하면 그날의 요일이 나오게 됨
string solution(int a, int b) {
int sum = b - 1;
for(int i = 0; i < a - 1; i++) sum += month[i];
answer = day[sum % 7];
return answer;
}
2. 완성 코드
#include <string>
#include <vector>
using namespace std;
string solution(int a, int b) {
string answer = "";
vector<string> day {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
vector<int> month {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum = b - 1;
for(int i = 0; i < a - 1; i++) sum += month[i];
answer = day[sum % 7];
return answer;
}