[프로그래머스 / C++] 개인정보 수집 유효기간

Taegang Yun·2023년 7월 7일
0
post-thumbnail

https://school.programmers.co.kr/learn/courses/30/lessons/150370?language=cpp#
2023 KAKAO BLIND RECRUITMENT

문자열 자르기와, map을 활용하면 쉽게 풀릴 줄 알았는데
샘플 케이스는 다 맞지만 제출했을 땐 40/100 점이 떴다.
왜 틀렸을까 고민중이다.

#include <string>
#include <vector>
#include <iostream>
#include <map>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    map<char, int > m;
    
    
    int today_year, today_month, today_date;
    today_year = stoi(today.substr(0, 4));
    today_month = stoi(today.substr(5, 2));
    today_date = stoi(today.substr(8, 2));
    //오늘의 연도, 월, 일
    
    
    for(int i = 0 ; i < terms.size(); i++){
        m[terms[i][0]] = stoi(terms[i].substr(2));
    }
    //map 활용 정보 저장
    
    for(int i = 0 ; i < privacies.size(); i++){
        int std_year = stoi(privacies[i].substr(0, 4));
        int std_month = stoi(privacies[i].substr(5, 2));
        int std_date = stoi(privacies[i].substr(8, 2));
        char std = privacies[i][11];
        
        if (std_date == 1){
            std_date = 28;
            std_month += (m[std] - 1 );
            if(std_month > 12){
                std_year += 1;
                std_month -= 12;
            }    
        }
        else{
            std_month += m[std];
            std_date -= 1;
            if(std_month > 12){
                std_year += 1;
                std_month -= 12;
            }    
        }
        
        //유효기간보다 오늘 날짜가 이후이면 파기해야함.
        
        if(today_year > std_year){ // 만약 오늘의 연도가 유효기간의 연도보다 크다면
            answer.push_back(i + 1);
            continue;
        }
        else if (today_year == std_year){
            if(today_month > std_month){
                answer.push_back(i + 1);
                continue;
            } 
            else if (today_month == std_month){
                if(today_date > std_date){
                    answer.push_back(i + 1);
                    continue;
                }
            }
        }      
    }
    
    
    return answer;
}

이렇게 했는데 40점이 나왔다. 어디가 틀렸을까

if(std_month > 12){
                int tmp = std_month / 12;
                std_year += tmp;
                std_month -= 12 * tmp;
            }    

이 부분을 고쳤더니 95점이 되었다. 남은 5점만 찾으면 된다.

틀린 부분을 찾았다.

        else{
            std_month += m[std];
            std_date -= 1;
            if(std_month > 12){
                int tmp = std_month / 12;
                std_year += tmp;
                std_month -= 12 * tmp;
                if(std_month == 0){ std_month = 12; std_year -= 1;}
            }    
        }

이렇게 바꿔주었다.

연, 월, 일 문제는 항상 이런걸 조심해야하는 것 같다.
일이 30일 넘어갈 때, 월이 12 넘어갈 때, 그리고 그걸 연도와 월수에 적용할 때.

100점 나왔다!!!

최종 코드

#include <string>
#include <vector>
#include <iostream>
#include <map>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    map<char, int > m;
    
    
    int today_year, today_month, today_date;
    today_year = stoi(today.substr(0, 4));
    today_month = stoi(today.substr(5, 2));
    today_date = stoi(today.substr(8, 2));
    //오늘의 연도, 월, 일
    
    
    for(int i = 0 ; i < terms.size(); i++){
        m[terms[i][0]] = stoi(terms[i].substr(2));
    }
    //map 활용 정보 저장
    
    for(int i = 0 ; i < privacies.size(); i++){
        int std_year = stoi(privacies[i].substr(0, 4));
        int std_month = stoi(privacies[i].substr(5, 2));
        int std_date = stoi(privacies[i].substr(8, 2));
        char std = privacies[i][11];
        
        if (std_date == 1){
            std_date = 28;
            std_month += (m[std] - 1 );
            if(std_month > 12){
                int tmp = std_month / 12;
                std_year += tmp;
                std_month -= 12 * tmp;
            }    
        }
        else{
            std_month += m[std];
            std_date -= 1;
            if(std_month > 12){
                int tmp = std_month / 12;
                std_year += tmp;
                std_month -= 12 * tmp;
                if(std_month == 0){ std_month = 12; std_year -= 1;}
            }    
        }
        
        //유효기간보다 오늘 날짜가 이후이면 파기해야함.
        
        if(today_year > std_year){ // 만약 오늘의 연도가 유효기간의 연도보다 크다면
            answer.push_back(i + 1);
            continue;
        }
        else if (today_year == std_year){
            if(today_month > std_month){
                answer.push_back(i + 1);
                continue;
            } 
            else if (today_month == std_month){
                if(today_date > std_date){
                    answer.push_back(i + 1);
                    continue;
                }
            }
        }      
    }
    
    
    return answer;
}
profile
언젠간 전문가가 되겠지

0개의 댓글

관련 채용 정보