-
백준 알고리즘, 1924 문제Algorithms/ACM_ICPC 2018. 7. 16. 14:44
개요
이 문제는 흔히 볼 수 있는 DATE 관련 문제이다.
입력되는 month, date 를 미리 정해논 기준으로 count 를 해줌으로서 해결할 수 있다.
풀이
먼저 상수 전역 변수로 DATE 와 요일을 나타내는 char 포인터 배열 를 정의 해놓는다.
그다음 1 월 1 일 부터 입력되는 요일까지를 count 해준다는 의미로 처음부터 count 변수를 생각했었기 때문에
count_date 변수에 누적되는 요일값을 더해줌으로써 해결했다.
/** * @site: https://www.acmicpc.net/problem/1924 * @github: https://github.com/7772 * @auth: Landon Park * @date: 2018. 07. 16 */ #include
using namespace std; const int DATE[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const char * DATE_THE_WEEK[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; int main() { int month, date; int i; int count_date = 0; cin >> month >> date; if(month < 1 || month > 12 || date < 1 || date > 31) return -1; for(i = 0; i < month - 1; i++) { count_date += DATE[i]; } count_date += date; cout << DATE_THE_WEEK[(count_date % 7)] << endl; return 0; } 물론 아래 코드도 가능하고, 공간도 더욱 절약할 수 있다.
int main() { int month, date; int i; cin >> month >> date; if(month < 1 || month > 12 || date < 1 || date > 31) return -1; for(i = 0; i < month - 1; i++) { date += DATE[i]; } cout << DATE_THE_WEEK[(date % 7)] << endl; return 0; }
count_date 변수 없이 그냥 date 변수에 누적해서 더해주면 된다.
Problem Solving 만을 놓고 생각해 보았을 때는 때때로 공간 복잡도의 제한도 까다롭게 여겨지기 때문에 아래 방법이 더 좋아보인다.
그러나 큰 프로그램을 개발할때에는
하나의 변수가 본래 목적을 잃고 변형되도록 개발하는 것은 그다지 좋은 습관은 아닌 듯 하다.
반응형'Algorithms > ACM_ICPC' 카테고리의 다른 글
백준 알고리즘, 11726 문제 (0) 2018.07.18 백준 알고리즘, 1463 문제 (0) 2018.07.17 백준 알고리즘, 입출력 관련문제 (0) 2018.07.17 백준 알고리즘, 11723 문제 (1) 2018.07.13 백준 알고리즘, 11720 문제 (2) 2018.07.13