문제 해결력 훈련

2. 나이 계산

Dev_Moses 2024. 1. 7. 22:17

 

#include <iostream>
using namespace std;

//atoi() : 문자형을 숫자형으로 변환
//itoa() : 숫자형을 문자형으로 변환

int main()
{
	string str;
	cin >> str;

	int Temp = 0;

	bool IsMan = true;

	// 0 : 아스키 코드 48
	for (int idx = 0; idx < str.size(); ++idx)
	{
		// 출생 년도 + 성별 구함 
		if (str[7] - 48 == 1)
		{
			Temp = 1900 + (10 * (str[0] - 48));
			Temp += (str[1] - 48);
			IsMan = true;
		}
		else if (str[7] - 48 == 2)
		{
			Temp = 1900 + (10 * (str[0] - 48));
			Temp += (str[1] - 48);
			IsMan = false;
		}
		else if (str[7] - 48 == 3)
		{
			Temp = 2000 + (10 * (str[0] - 48));
			Temp += (str[1] - 48);
			IsMan = true;
		}
		else
		{
			Temp = 2000 + (10 * (str[0] - 48));
			Temp += (str[1] - 48);
			IsMan = false;
		}
	}

	// 나이 출력 
	cout << 2019 - Temp + 1 << " ";
	
	// 성별 출력
	if (IsMan == true)
	{
		cout << "M";
	}
	else
	{
		cout << "W";
	}
}