코딩테스트 (71) 썸네일형 리스트형 프로그래머스_2단계_구명보트_C++ 그리디 알고리즘을 활용하는문제 문제풀이: #include #include using namespace std; int solution(vector people, int limit) { int answer = 0; sort(people.begin(), people.end()); // 사람들의 몸무게를 오름차순으로 정렬 int left = 0; // 가장 가벼운 사람을 가리키는 인덱스 int right = people.size() - 1; // 가장 무거운 사람을 가리키는 인덱스 while (left 프로그래머스_1단계_문자열 내 p와 y의 개수_C++ 문제풀이: #include #include #include // islower, isupper 함수 사용을 위한 헤더 파일 using namespace std; bool solution(string s) { int np = 0; int ny = 0; // 문자열을 순회하면서 'p'와 'y'의 개수를 세기 for (char c : s) { // 대소문자 구분 없이 'p'와 'y'의 개수 세기 if (tolower(c) == 'p') { np++; } else if (tolower(c) == 'y') { ny++; } } // 'p'와 'y'의 개수 비교하여 결과 반환 return np == ny; } int main() { cout 프로그래머스_1단계_내적_C++ 문제풀이: #include #include using namespace std; int solution(vector a, vector b) { //int answer = 1234567890; int answer = 0; for(int i=0; i 프로그래머스_1단계_음양 더하기_C++ 문제풀이:3번풀이가 최적화 된 코드이다 최적화 안된거같으니까 삼항연산자 활용해보자 문제풀이1 #include #include using namespace std; int solution(vector absolutes, vector signs) { int answer = 0; for( int i=0; i 프로그래머스_0단계_ad 제거하기_C++ 문제풀이: 이문제는 if(s.find("ad") == string::npos)를 이해해야한다. #include #include using namespace std; vector solution(vector strArr) { vector answer; for(string s : strArr) { if(s.find("ad") == string::npos) { answer.push_back(s); } } return answer; } string::npos는 C++ 표준 라이브러리에서 제공하는 string 클래스의 정적 상수이다. 이 값은 string 클래스에서 문자열을 찾지 못한 경우 반환되는 특별한 값이고 string::npos의 값은 보통 -1로 정의되어 있으며, find() 함수가 문자열을 찾지 못한 .. 프로그래머스_0단계_공백으로 구분하기 1C++ 문제풀이: 문자열 스트림 istringstream #include #include #include using namespace std; vector solution(string my_string) { vector answer; // 문자열 스트림 생성 istringstream iss(my_string); string word; // 공백을 기준으로 문자열을 분리하여 각 단어를 추출하고 배열에 담음 while (iss >> word) { answer.push_back(word); } return answer; } 만약 문자열 스트림을 사용하지 않았을때의 코드 #include #include using namespace std; vector solution(string my_string) { vector a.. 프로그래머스_0단계_숨어있는 숫자의 덧셈 (1)C++ 문제풀이: #include #include using namespace std; int solution(string my_string) { int answer = 0; for(char c: my_string) { if(c>='0'&& c 프로그래머스_0단계_문자열 정렬하기 (1)C++ 문제풀이: #include #include #include using namespace std; vector solution(string my_string) { vector answer; for(char c: my_string) { if(c>='0' && c 이전 1 2 3 4 5 6 ··· 9 다음