반응형
문제링크
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제
코드,풀이
이 문제는 문자열을 뽑는 문제로 귀찮은 구분자를 기준으로 뽑을 필요가 없이 달(month)나 일수가 한자리일때 앞에 0을 친절하게 붙혀주는 문제였습니다.
그래서 저는 substr를 활용하여 원하는 위치의 인덱스들의 문자열들을 뽑아와 stoi를 해주었습니다.
사실 틀은 하라는대로 하기만하고 따로 알고리즘을 엄청나게 요구하지 않아 아래의 코드를 보시게 되면 이해가 되실거라고 생각합니다 🙂
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
map<char,int> m; // 약관, 기관 저장 맵
int todayYear = stoi(today.substr(0,4));
int todayMonth = stoi(today.substr(5,7));
int todayDay = stoi(today.substr(8,10));
for(int i = 0; i < terms.size();i++){
m[terms[i][0]] = stoi(terms[i].substr(2));
}
for(int i = 0; i < privacies.size();i++){
int pastYear = stoi(privacies[i].substr(0,4));
int pastMonth = stoi(privacies[i].substr(5,7));
int pastDay = stoi(privacies[i].substr(8,10));
char term = privacies[i][11];
int difference = 0;
difference = (todayYear - pastYear) * 12; //월별로 계산을 위해
difference += todayMonth - pastMonth;
difference *= 28;
difference += todayDay - pastDay;
if(m[term] * 28 <= difference){
answer.push_back(i + 1);
}
}
return answer;
}
반응형
'알고리즘 > c++ 프로그래머스' 카테고리의 다른 글
[프로그래머스 완주하지 못한 참가자] c++ (풀이,코드) (0) | 2023.07.09 |
---|---|
[프로그래머스 섬 연결하기] c++ (풀이,코드, 크루스칼 알고리즘) (0) | 2023.07.06 |
[프로그래머스 신고결과받기] c++ (풀이,코드) (0) | 2023.07.05 |
[프로그래머스 가장 먼 노드] c++ (풀이,코드, bfs) (0) | 2023.07.04 |
[프로그래머스 크레인 인형뽑기 게임] c++ (풀이,코드,스택) (0) | 2023.07.04 |
댓글