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

 

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

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

 

26576번: Date

You are starting to get annoyed with how your phone shows you the date, so you want to put it into a different format. Currently, your phone gives you the date in the format Month Day, Year (ex. July 25, 2018). Your goal is to shorten the format exactly li

www.acmicpc.net

1월부터 12월까지 각 단어와 그를 숫자를 이용해 나타난 표기를 map을 이용해 연결시켜두면 잘못된 표기의 달이 들어오는지의 여부와 이를 정수 두자리로 표기하는 것을 간단히 구현할 수 있다.

 

이 문제에서 날짜를 나타내는 수는 1 이상 31 이하이면 정상으로 처리하라는 지문의 조건에 유의하자.

 

출력 시 각 정수는 항상 두 자리를 표기해야함에 유의하자. 즉 표기할 수가 한자리라면 0을 앞에 써붙여서 출력해야 함에 유의하자.

 

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

#include <iostream>
#include <map>
#include <string>
using namespace std;

int T;
map<string, string> mp;

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

	mp.insert({ "January","01" });
	mp.insert({ "February","02" });
	mp.insert({ "March","03" });
	mp.insert({ "April","04" });
	mp.insert({ "May","05" });
	mp.insert({ "June","06" });
	mp.insert({ "July","07" });
	mp.insert({ "August","08" });
	mp.insert({ "September","09" });
	mp.insert({ "October","10" });
	mp.insert({ "November","11" });
	mp.insert({ "December","12" });

	cin >> T;
	while (T--) {
		string M, D, Y; cin >> M >> D >> Y;
		D.pop_back();
		int DD = stoi(D);
		if (DD < 10) D = "0" + D;
		Y = "0" + Y;
		if (mp.count(M) && 0 < DD && DD < 32) {
			cout << mp[M] << '/' << D << '/' << Y.substr(Y.length() - 2, 2) << '\n';
		}
		else cout << "Invalid\n";
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 26548 // C++] Quadratics  (0) 2022.12.21
[BOJ 26577 // C++] Math  (0) 2022.12.20
[BOJ 24451 // C++] 飴 2 (Candies 2)  (0) 2022.12.20
[BOJ 6888 // C++] Terms of Office  (0) 2022.12.20
[BOJ 2380 // Befunge] Star  (0) 2022.12.20

+ Recent posts