[Level 1, C++] 문자열 다루기 기본
·
정리 전 게시글/공부 관련
#include #include using namespace std; bool solution(string s) { if(s.length() == 4 || s.length() == 6) { if (s.length() == to_string(atoi(s.c_str())).length()) return true; else return false; } else return false; }
[Level 1, C++] 나누어 떨어지는 숫자 배열
·
정리 전 게시글/공부 관련
#include #include #include using namespace std; vector solution(vector arr, int divisor) { vector answer; for(int & i : arr) { if(i % divisor == 0) answer.push_back(i); } sort(answer.begin(), answer.end()); if(answer.size() == 0) answer.push_back(-1); return answer; }
[Level 1, C++] 부족한 금액 계산하기
·
정리 전 게시글/공부 관련
for문의 조건을 잘 만들면 쉽게 만들 수 있다. long long으로 하지 않으면 오류가 난다. using namespace std; long long solution(int price, int money, int count) { long long answer = 0; for(long long i=price; i= money ? answer -= money : answer = 0; return answer; }
[Level 1, C++] 문자열 내 마음대로 정렬하기
·
정리 전 게시글/공부 관련
sort 함수를 이용하는데 정렬 기준을 내가 만들면 쉽게 만들 수 있다. #include #include #include using namespace std; int i; bool compare(string s1, string s2) { if(s1[i] == s2[i]) { if(s1 < s2) return true; else return false; } else if(s1[i] < s2[i]){ return true; } else return false; } vector solution(vector strings, int n) { i = n; vector answer = strings; sort(answer.begin(), answer.end(), compare); return answer; }
[Level 1, C++] 최소직사각형
·
정리 전 게시글/공부 관련
모든 지갑을 같은 방향으로 만들어 놓으면 계산하기 편하다. #include #include using namespace std; int solution(vector sizes) { int answer = 0; int temp = 0; vector max(2, 0); for(int i=0; i
[Level 1, C++] 2016년
·
정리 전 게시글/공부 관련
시간을 계산해주는 방법을 찾아서 2016년 1월 1일 부터 해당 날짜까지 시간을 계산해서 요일을 구한다. #include #include #include #include using namespace std; string solution(int a, int b) { string answer = ""; time_t start, end; struct tm s_time; int tm_day, tm_hour, tm_min, tm_sec; double diff; string day[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; s_time.tm_year = 2016 - 1900; s_time.tm_mon = 1 - 1; s_time.tm_mday = 1; s..
[Level 1, C++] 두 개 뽑아서 더하기
·
정리 전 게시글/공부 관련
중복을 제거하는 함수인 unique와 erase를 같이 쓰면 쉽게 풀 수있다. #include #include #include using namespace std; vector solution(vector numbers) { vector answer; for(int i=0; i
[Level 1, C++] 3진법 뒤집기
·
정리 전 게시글/공부 관련
파이썬에는 진법 계산하는 함수같은게 있던데 c++엔 그런게 없다. 그래서 그냥 직접 진법 계산하는걸로 만들었다. 직접 계산하면 앞뒤로 뒤집을 필요없으니 좋았다. #include #include #include #include using namespace std; int solution(int n) { int answer = 0; string str = ""; int i = 0; while (true) { str.append(to_string(n % 3)); n = n / 3; if( n == 0) break; } for (int i = 0; i < str.length(); i++) { cout