프로그래머스 :연습문제 [16]
- < 프로그래머스 :두 정수 사이의 합>

#### 해결방법
- a,b중 큰수가 a, 작은수가 b가 오도록swap
- a~b까지 for문으로 돌아가면서 answer에 값을 더해준다.
### solution
#include <string>
#include <vector>
using namespace std;
void swap(int& a, int& b);
long long solution(int a, int b) {
long long answer = 0;
if (a > b) {
swap(a, b);
}
for (int i = a; i <= b; i++) {
answer += i;
}
return answer;
}
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
-
< 프로그래머스 :문자열 내 마음대로 정렬하기>

해결방법
- cmp함수를 만들어서 sort함수를 사용한다.
- 만약 n번째 인덱스의 문자가 같으면 사전순으로 정렬하고 그렇지 않으면 n번쨰 기준 문자대로 정렬한다.
solution
#include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; int num=0; bool cmp(string a, string b) { if (a[num] == b[num]) { return 1; } return a[num] < b[num]; } vector<string> solution(vector<string> strings, int n) { vector<string> answer; num = n; sort(strings.begin(), strings.end(), cmp); return strings; }
-
< 프로그래머스 :서울에서 김서방 찾기>

#### 해결방법 - find함수를 이용하여Kim이 존재하는 iter위치를 받아온다. - 벡터의 시작점과 iter의 거리를 distance함수를 이용해 알아와 index를 구한다. #### 참고 - find 함수 https://hyeonstorage.tistory.com/319 ### solution ```cpp#include
#include #include #include using namespace std; string solution(vector<string> seoul) { vector<string>::iterator iter = std::find(seoul.begin(), seoul.end(), "Kim"); string answer = ""; int index = distance(seoul.begin(), iter); answer = "김서방은 " + to_string(index) + "에 있다"; return answer; } ```