본문 바로가기

전체 글

(110)
프로그래머스_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++ 문제풀이: #include using namespace std; long long solution(int price, int money, int count) { long long total_price = 0; for (int i = 1; i 0 ? answer : 0); } 문제푸는동안 틀렸던점 while로 카운트를 감소시키면서 0보다클때를 했다 이렇게 반복문을 돌리면 최적화가 안될뿐더러 비효율적이다 using namespace std; long long solution(int price, int money, int count) { long long answer = -1; int value = 1; while(count>0) { money -= price*count; count--; } if(money
프로그래머스_1단계_음양 더하기_C++ 문제풀이:3번풀이가 최적화 된 코드이다 최적화 안된거같으니까 삼항연산자 활용해보자 문제풀이1 #include #include using namespace std; int solution(vector absolutes, vector signs) { int answer = 0; for( int i=0; i
프로그래머스_1단계_제일 작은 수 제거하기_C++ 문제풀이:lier활용하기 / min_elemnet 함수를 활용하여 (가장작은값의 lier알아내기) #include #include #include using namespace std; vector solution(vector arr) { vector answer; if (arr.size() > 1) { // 가장 작은 수의 iterator 찾기 auto min_iter = min_element(arr.begin(), arr.end()); // 가장 작은 수를 제외한 나머지 요소들을 answer에 복사 answer.reserve(arr.size() - 1); for (auto it = arr.begin(); it != arr.end(); ++it) { if (it != min_iter) { answer.p..
프로그래머스_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