백준 10426번 : 기념일 2

M1ndCon·2024년 6월 26일
0

Algorithm

목록 보기
7/32

  • Solved.ac 기준 브론즈 1
  • 사용언어 C++

문제 해석

  1. 윤년을 고려하여 계산
  2. 찾고자 하는게 몇일차(N)인지 계산

문제 풀이

  1. YYYY%4 == 0 && (YYYY%100 != 0 || YYYY%400 == 0)
  2. 현재 날짜의 N-1일을 더함 : 첫날이 1일차이기 때문에
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

int year, month, day;
string str_Date;
int n;


bool isLeapYear(int year) {
	if (year % 4 == 0) {
		if (year % 100 == 0) {
			return year % 400 == 0;
		}
		return true;
	}
	return false;
}

int daysInMonth(int year, int month) {
	switch (month)
	{
		case 1: return 31;
		case 2: return isLeapYear(year) ? 29 : 28;
		case 3: return 31;
		case 4: return 30;
		case 5: return 31;
		case 6: return 30;
		case 7: return 31;
		case 8: return 31;
		case 9: return 30;
		case 10: return 31;
		case 11: return 30;
		case 12: return 31;
		default: return 0;
	}
}

vector<string> split(string input, char delimiter) {
	vector<string> answer;
	stringstream ss(input);
	string temp;

	while (getline(ss, temp, delimiter)) {
		answer.push_back(temp);
	}

	return answer;
}

int main() {
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	cin >> str_Date >> n;;

	vector<string> arr = split(str_Date, '-');

	year = stoi(arr[0]);
	month = stoi(arr[1]);
	day = stoi(arr[2]);

	day += n - 1;
	while (day > daysInMonth(year, month)) {
		day -= daysInMonth(year, month);
		month++;
		if (month > 12) {
			month = 1;
			year++;
		}
	}


	if (month < 10) {
		if (day < 10) {
			cout << year << "-0" << month << "-0" << day;
		}
		else {
			cout << year << "-0" << month << "-" << day;
		}
	}
	else {
		if (day < 10) {
			cout << year << "-" << month << "-0" << day;
		}
		else {
			cout << year << "-" << month << "-" << day;
		}
	}
	
	
	
	return 0;
}
profile
게임 개발자 지망생

0개의 댓글