728x90 반응형 프로그래머스 C++65 프로그래머스 Level.0 자릿수 더하기 정수 n이 매개변수로 주어질 때 n의 각 자리 숫자의 합을 return하도록 solution 함수를 완성해주세요 #include #include using namespace std; int solution(int n) { int answer = 0; string a = ""; a = to_string(n); for(int i = 0; i < a.size(); i++){ answer += a[i] - '0'; } return answer; } 정수를 문자열로 바꾼 후 그 안의 인덱스 값만큼 반복하여 배열의 원소들을 계속 더해주는 방법으로 풀었다. 왜 이 문제를 풀었던것 같은 기분이 들까? 그런데 프로그래머스 안푼 문제로 되어있다. 무슨일이오! 2023. 8. 11. 프로그래머스 Level.0 숨어있는 숫자의 덧셈 (1) 문자열 my_string이 매개변수로 주어집니다. my_string안의 모든 자연수들의 합을 return하도록 solution 함수를 완성해주세요. #include #include #include using namespace std; int solution(string my_string) { int answer = 0; for(int i = 0; i < my_string.size(); i++){ if(isdigit(my_string[i])){ answer += (my_string[i] - '0'); } } return answer; } 문자열 안에 있는 원소가 정수인지 문자인지 판별하는 함수가 필요했다. atoi, isdigit 이렇게 두가지를 발견했고, isdigit 함수를 사용하기로 했다 이 함수에 .. 2023. 8. 2. 프로그래머스 Level.1 문자열을 정수로 바꾸기 stoi() 함수를 사용해서 바꿔주는 것으로 문제를 풀었다. #include #include using namespace std; int solution(string s) { int answer = 0; answer = stoi(s); return answer; } https://yeni-0224.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-C-Level-0-%EB%AC%B8%EC%9E%90%EC%97%B4%EB%A1%9C-%EB%B3%80%ED%99%98 프로그래머스 C++ Level. 0 문자열로 변환 정수 n이 주어질 때, n을 문자열로 변환하여 return하도록 solution 함수를 완성해주세요. #includ.. 2023. 7. 10. 프로그래머스 Level.1 자연수 뒤집어 배열로 만들기 당황스럽다. 이렇게 비어있는데 질문이 47개나 적혀있다는 것.... https://hwan-shell.tistory.com/119 C++ vector사용법 및 설명 (장&단점) C++의 vector는 C++ 표준라이브러리(Standard Template Library)에 있는 컨테이너로 사용자가 사용하기 편하게 정의된 class를 말합니다. vector를 생성하면 메모리 heap에 생성되며 동적할당됩니다. 물론 속도 hwan-shell.tistory.com https://www.delftstack.com/ko/howto/cpp/how-to-convert-vector-to-array-in-cpp/ C++에서 벡터를 배열로 변환하는 방법 이 기사에서는 C++에서 벡터를 배열로 변환하는 방법을 소개합니다. w.. 2023. 7. 10. 프로그래머스 Level.1 문자열 내 p와 y의 개수 대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다. #include #include using namespace std; bool solution(string s) { bool answer = true; int pCount = 0; int yCount = 0; for(int i = 0; i < s.size(); i++) { if(s[i] == 'p' || s[i] == 'P') pCount++; else if(s[i] == 'y'|| s[i] ==.. 2023. 7. 8. 프로그래머스 Level.1 나누어 떨어지는 숫자 배열 array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요. divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하세요. #include #include #include using namespace std; vector solution(vector arr, int divisor) { vector answer; for(int i = 0; i < arr.size(); i++) { if(arr[i] % divisor == 0) { answer.push_back(arr[i]); } } sort(answer.begin(), answer.end()); if(answer.empty()) { answ.. 2023. 7. 7. 이전 1 2 3 4 5 6 7 ··· 11 다음 728x90 반응형