https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
카카오 문제라 그런지 단순히 내지는 않는 것 같다. 그래도 쉬운 문제다.
today, terms, privacies 변수 모두 일정한 길이를 가지므로 string의 substr을 사용해 날짜, 계약, 기간을 분리한다.
convert 함수를 사용해서 정수형으로 바꿔주고, check 함수에서 일 단위로 바꿔서 크거나 같다면 계약기간이 만료된 것이므로 answer에 추가한다.
#include <string>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
struct Info{
int year, month, day;
};
Info convert(string& s){
Info info;
info.year = stoi(s.substr(0,4));
info.month = stoi(s.substr(5,2));
info.day = stoi(s.substr(8,2));
return info;
}
bool check(Info& today, Info& pday, int due){
// 파기
int today_to_day = today.year*12*28 + today.month * 28 + today.day;
int c_to_day = pday.year*12*28 + pday.month*28 + pday.day + due*28;
if (today_to_day >= c_to_day)
return true;
else return false;
}
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
map<string, string> m;
Info n_today;
n_today = convert(today);
for (int i = 0; i < terms.size(); i++) {
string num="", s="";
s = terms[i].substr(0,1);
num = terms[i].substr(2);
m.insert({s, num});
}
Info p_day;
string p_kind="";
int p_due=0;
for (int i = 0; i < privacies.size(); i++) {
string s = privacies[i].substr(0, 10);
p_day = convert(s);
p_kind = privacies[i].substr(11, 1);
p_due = stoi(m[p_kind]);
if (check(n_today, p_day, p_due)) answer.push_back(i+1);
}
return answer;
}
'프로그래머스 > Lv.1' 카테고리의 다른 글
[C++] 프로그래머스 소수 만들기 [재귀] (0) | 2023.03.05 |
---|---|
[C++] 프로그래머스 폰켓몬 (0) | 2023.03.02 |
[C++] 프로그래머스 둘만의 암호 (0) | 2023.02.28 |
[C++] 프로그래머스 대충 만든 자판 [해쉬, Hash] (0) | 2023.02.28 |
[C++] 프로그래머스 햄버거 만들기 [벡터 or 문자열 활용] (0) | 2023.02.28 |