※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 3183번 문제인 Dates이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/3183 

 

3183번: Dates

Input will consist of a number of dates, each on a separate line. Each date will consist of three non-negative integers, the first to represent the day, the second the month and the third the year. The day and month will always be less than 100, and the ye

www.acmicpc.net

입력으로 주어지는 날짜가 유효한 날짜인지 확인하는 문제이다.

 

입력으로 주어지는 달이 1 이상 12 이하인지를 먼저 확인하자. 그리고 각 달의 일수에 맞춰 주어진 일이 유효한지를 확인하자. 이는 각 달이 며칠까지 있는지를 저장한 배열을 만들어 편하게 구현할 수 있다. 이 때 2월의 경우 윤년 여부에 따라 29일이 존재하기도 하고 안 하기도 하므로 예외처리를 해주자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int Y, M, D;
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> D >> M >> Y;
	while (D + M + Y) {
		bool chk = 1;
		if (M < 1 || M > 12) chk = 0;
		else if (M == 2) {
			if (Y % 4 == 0 && (Y % 100 || Y % 400 == 0)) {
				if (D < 1 || D > 29) chk = 0;
			}
			else {
				if (D < 1 || D > 28) chk = 0;
			}
		}
		else {
			if (D < 1 || D > days[M]) chk = 0;
		}

		if (chk) cout << "Valid\n";
		else cout << "Invalid\n";

		cin >> D >> M >> Y;
	}
}
728x90

+ Recent posts