2025. 4. 16.


1차로 작성한건데
signal: aborted (core dumped)
이거 뜬다
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
map <char, int> m;
for(auto x : terms){m[x[0]] = stoi(x.substr(2,x.size()-2));}
int cnt = 0;
today = today.substr(2,2)+today.substr(5,2)+today.substr(8,2);
// 비교하기 편하게 오늘 날짜 yymmdd꼴로 만들기
for(auto p : privacies){
cnt++; // 입력할 cnt
string day = p.substr(2,2)+p.substr(5,2);
if(day[4] == '0' && day[5] == '1'){day[3]--; day[4] ='2'; day[5]='8';}
// 비교하기 편하게 약관 날짜 yymmdd꼴로 만들고 1일 빼기
int N = m[p[11]];
while(N--){
if(day[2]=='1' && day[3] >= '2'){
if(day[1] != '9') day[1]++;
else {day[0]++; day[1]='0';}
day[2]='0'; day[3]='1';
}
else if(day[2]=='0' && day[3] =='9'){
day[2]++; day[3]='0';
}
else day[3]++;
}
// 약관 날짜에 우효기간 더하기
if (stoi(day) < stoi(today)) answer.push_back(cnt);
}
return answer;
}
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> p) {
vector<int> answer;
map <char, int> m;
for(auto x : terms){m[x[0]] = stoi(x.substr(2,x.size()-2));}
// 약관종류량 유효기간 정리
today = today.substr(2,2)+today.substr(5,2)+today.substr(8,2);
// 비교하기 편하게 오늘 날짜 yymmdd꼴로 만들기
for(int i =0; i<p.size(); ++i){
string day = ""; // 비교할거
if(p[i].substr(8,2) == "01"){
p[i][6]--; p[i][8] = '2'; p[i][9] = '9';
} // dd부분 -1 할거 고려해서 1일일때 바꿔놓기
int month = stoi(p[i].substr(5,2)) + m[p[i][11]]; // mm부분 (유효기간 정한거
int yy = stoi(p[i].substr(2,2))+int(month/12);
if(month%12==0) day = to_string(yy-1)+"12"+ p[i].substr(8,2);
else if(month%12<10) day = to_string(yy)+'0'+to_string(month%12)+ p[i].substr(8,2);
else day = to_string(yy)+to_string(month%12)+ p[i].substr(8,2);
cout << day << "*****\n";
if (stoi(day)-1 < stoi(today)) answer.push_back(i+1);
}
return answer;
}
17번 케이스에서만 오류가 난다
근데 생각해보니까 일을 -1하지말고
그냥 월만 계산한 다음에
if (stoi(day)-1 < stoi(today)) answer.push_back(i+1);
이거를
if (stoi(day) <= stoi(today)) answer.push_back(i+1);
이렇게 하면 되는거 아닌가?
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> p) {
vector<int> answer;
map <char, int> m;
for(auto x : terms){m[x[0]] = stoi(x.substr(2,x.size()-2));}
// 약관종류량 유효기간 정리
today = today.substr(2,2)+today.substr(5,2)+today.substr(8,2);
// 비교하기 편하게 오늘 날짜 yymmdd꼴로 만들기
for(int i =0; i<p.size(); ++i){
string day = ""; // 비교할거
int month = stoi(p[i].substr(5,2)) + m[p[i][11]]; // mm부분 (유효기간 정한거
int yy = stoi(p[i].substr(2,2))+int(month/12);
if(month%12==0) day = to_string(yy-1)+"12"+ p[i].substr(8,2);
else if(month%12<10) day = to_string(yy)+'0'+to_string(month%12)+ p[i].substr(8,2);
else day = to_string(yy)+to_string(month%12)+ p[i].substr(8,2);
if (stoi(day) <= stoi(today)) answer.push_back(i+1);
}
return answer;
}'C++ 문제풀기 > 백준-프로그래머스' 카테고리의 다른 글
| 백준 | 4963. 섬의 개수 (1) | 2025.06.24 |
|---|---|
| 백준 | 2667. 단지번호 붙이기 (3) | 2025.06.24 |
| 프로그래머스 | 택배상자꺼내기 (2) | 2025.06.19 |
| 프로그래머스 | 키패드 누르기 (0) | 2025.06.19 |
| 프로그래머스 | 나선형으로 배열하기 (0) | 2025.06.17 |