본문 바로가기

C++/프로그래머스

(18)
[프로그래머스] [C++] 오픈채팅방 문제 풀이 #include #include #include // stringstream #include // map using namespace std; // 문자열을 나누어 저장해주는 함수 split. 뭔지 모르겠으면 python의 split을 찾아보자 vector split(string input, char delimiter) { vector ans; stringstream ss(input); string tmp; while (getline(ss, tmp, delimiter)) { ans.push_back(tmp); } return ans; } vector solution(vector record) { // record를 나누어 저장할 이차원 배열 vector uid; // record를 나누어 ui..
[프로그래머스] [C++] 카펫 문제 풀이 #include #include using namespace std; vector solution(int brown, int red) { vector answer; // 카펫의 전체 크기는 brown + yellow다. int area = brown + yellow; // yellow가 최소(1)일 때 area는 3 * 3의 크기를 가진다. 이는 width의 최솟값이 3이고, 최댓값이 area / 3이라는 것을 말한다. // 가로가 세로보다 같거나 커야 하기 때문에 최댓값부터 내려가는 식으로 for문을 구현한다. for (int width = area / 3; width >= 3; width--) { // area가 가로 값으로 나누어 떨어지면 if (area % width == 0) { //..
[프로그래머스] [C++] 소수 찾기 문제 풀이 #include #include #include #include using namespace std; int solution(string numbers) { int answer = 0; // string 타입의 numbers를 int로 받아올 벡터 생성 vector num; // 현재 numbers는 string 형태이기 때문에 int형으로 받아오게 되면 숫자가 달라진다. ex) string 타입의 "0"은 int타입으로 48. // 때문에 '0'을 빼서 int형태에 맞게 숫자를 맞춰준다. for (auto it : numbers) num.push_back(it - '0'); // next_permutation을 사용하기 위해 오름차순으로 정렬해준다. 왜냐하면 // next_permutatio..
[프로그래머스] [C++] 모의고사 문제 풀이 #include #include #include using namespace std; vector solution(vector answers) { vector answer; // 수포자의 찍는 방식 vector s = { {1, 2, 3, 4, 5} , {2, 1, 2, 3, 2, 4, 2, 5} , {3, 3, 1, 1, 2, 2, 4, 4, 5, 5} }; // 각 수포자의 점수 vector v = { 0, 0, 0 }; // 수포자의 인원만큼 돌면서 for (int i = 0; i < s.size(); i++) { // 정답을 확인하는데 for (int j = 0; j < answers.size(); j++) { // 첫 번째 수포자이고, 이 사람의 찍는 방식은 5를 주기로 계속 돈다. ..
[프로그래머스] [C++] H-Index 문제 풀이 #include #include #include using namespace std; int solution(vector citations) { int answer = 0; // citations를 내림차순으로 정렬 sort(citations.begin(), citations.end(), greater()); // citations는 현재 가장 큰 수부터 작아지는 순으로 정렬되어 있다. // citations[i]는 인용된 횟수이고, i는 논문의 개수이다. // 즉, 인용된 횟수 citations[i](h)가 i(h)편 이상이기 위해서는 // citations[i] > i 를 충족해야 한다. for (int i = 0; i < citations.size(); i++) if (citations[..
[프로그래머스] [C++] 가장 큰 수 문제 풀이 #include #include #include using namespace std; // 배열에서 인접한 두 원소를 더하여 그 중 더 큰 순서대로 원소를 배열한다. // ex) '6' + '10'= '610' | '10' + '6' = '106' // string 형태이기 때문에 사용 가능하다. bool st_cmp(string a, string b) { return a + b > b + a; } string solution(vector numbers) { string answer = ""; vector s; // numbers에 있는 원소들을 string 타입으로 변경하여 s에 삽입 for (int i = 0; i < numbers.size(); i++) { string tmp = to_s..
[프로그래머스] [C++] K번째 수 문제 풀이 #include #include #include using namespace std; vector solution(vector array, vector commands) { vector answer; // 자동 정렬을 위한 우선순위 큐 생성. 오름차순으로 사용하기 위해 greater 사용 priority_queue pq; for (int i = 0; i < commands.size(); i++) { // command[i][0]부터 command[i][1]까지 우선순위 큐에 넣는다. // j-1을 하는 이유는 배열의 인덱스가 0부터 시작하기 때문. for (int j = commands[i][0]; j
[프로그래머스] [C++] 이중우선순위큐 문제 풀이 #include #include #include #include using namespace std; vector solution(vector operations) { vector answer; // 자동 정렬을 위한 우선순위 큐 생성. 우선순위 큐는 "내림차순"으로 정렬된다! priority_queue pq; // operations를 돌면서 for (int i = 0; i < operations.size(); i++) { // 명렁어가 I 숫자라면 if (operations[i][0] == 'I') { // 공백 이후 숫자부분(substr(2))을 숫자로 바꾸어(stoi) 우선순위 큐에 저장 pq.push(stoi(operations[i].substr(2))); } // 우선순위 큐에 값이..