본문 바로가기
알고리즘/c++ 프로그래머스

[프로그래머스 개인정보 수집기간] c++ (풀이,코드)

by 말린밴댕이_공부 2023. 7. 5.
반응형

문제링크

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;
}
반응형

댓글