본문 바로가기
728x90
반응형

프로그래머스 C++/Level.037

프로그래머스 C++ Level. 0 문자열로 변환 정수 n이 주어질 때, n을 문자열로 변환하여 return하도록 solution 함수를 완성해주세요. #include #include using namespace std; string solution(int n) { string answer = ""; answer = to_string(n); return answer; } 프로그래머스에 새로운 문제도 떴길래 바로 하나 선택해서 풀어보았다. 정수형을 문자열로 변환해주는 것이 키포인트였다. to_string() 함수를 알았다면 바로 해결할 수 있는 문제였다고 생각한다. 정수 >> 문자열 : to_string() 문자열 >> 정수 : stoi() https://notepad96.tistory.com/67 C++ String to int, int to Strin.. 2023. 4. 26.
프로그래머스 C++ Level. 0 중앙값 구하기 중앙값은 어떤 주어진 값들을 크기의 순서대로 정렬했을 때 가장 중앙에 위치하는 값을 의미합니다. 예를 들어 1, 2, 7, 10, 11의 중앙값은 7입니다. 정수 배열 array가 매개변수로 주어질 때, 중앙값을 return 하도록 solution 함수를 완성해보세요. #include #include #include using namespace std; int solution(vector array) { int answer = 0; sort(array.begin(), array.end()); int a = array.size() * 0.5; answer = array[a]; return answer; } 숫자 크기 순으로 나열하고 배열의 가운데 인덱스를 int a에 담아주었다. 배열 인덱스의 값을 ans.. 2023. 4. 25.
프로그래머스 C++ Level. 0 특정 문자 제거하기 문자열 my_string과 문자 letter이 매개변수로 주어집니다. my_string에서 letter를 제거한 문자열을 return하도록 solution 함수를 완성해주세요. #include #include using namespace std; string solution(string my_string, string letter) { string answer = ""; for(int i = 0 ; i < my_string.size(); i++){ if(my_string[i] != letter[0]) { answer += my_string[i]; } } return answer; } 생각의 전환이 필요하다. (정답을 검색해버렸다) 검색해버린 이유 : 구글에서 사용하라던 erase, std::remove .. 2023. 4. 12.
프로그래머스 C++ Level. 0 삼각형의 완성조건(1) 선분 세 개로 삼각형을 만들기 위해서는 다음과 같은 조건을 만족해야 합니다. - 가장 긴 변의 길이는 다른 두 변의 길이의 합보다 작아야합니다. 삼각형의 세 변의 길이가 담긴 배열 sides 이 매개변수로 주어집니다. 세 변으로 삼각형을 만들 수 있다면 1, 만들 수 없다면 2를 return하도록 solution 함수를 완성해주세요. #include #include #include using namespace std; int solution(vector sides) { int answer = 0; sort(sides.begin(), sides.end()); answer = sides[0] + sides[1] > sides[2] ? 1 : 2; return answer; } sort 함수로 배열 원소의 사.. 2023. 4. 11.
프로그래머스 C++ Level. 0 배열의 유사도 두 배열이 얼마나 유사한지 확인해보려고 합니다. 문자열 배열 s1과 s2가 주어질 때 같은 원소의 개수를 return하도록 solution 함수를 완성해주세요. #include #include using namespace std; int solution(vector s1, vector s2) { int answer = 0; vector a = s1.size() 2023. 4. 10.
프로그래머스 C++ Level. 0 최댓값 만들기(1) 정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소 중 두 개를 곱해 만들 수 있는 최댓값을 return하도록 solution 함수를 완성해주세요. #include #include #include using namespace std; int solution(vector numbers) { int answer = 0; sort(numbers.begin(), numbers.end()); answer = numbers[numbers.size() - 1] * numbers[numbers.size() - 2]; return answer; } 이제는 c++에서 제공하는 함수를 열심히 사용하기로 했다. 내가 나아가야할 길... 그것은 제공되는 함수를 열심히 공부하고 연습하는 것...! sort 함.. 2023. 4. 3.
728x90
반응형