본문 바로가기
728x90
반응형

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

프로그래머스 Level1. 두 정수 사이의 합 두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. #include #include using namespace std; long long solution(int a, int b) { long long answer = 0; if(abs(b - a) == 0) answer = a; else{ for(int i = 0; i b){ answer += (b + i); } else{ answer += (a + i); } } } return answer; } a와 b의 값이 같은 경우는 a 나 b 둘.. 2023. 8. 30.
프로그래머스 Level.1 하샤드 수 양의 정수 x가 하샤드 수이려면 x의 자릿수의 합으로 x가 나누어져야 합니다. 예를 들어 18의 자릿수 합은 1+8=9이고, 18은 9로 나누어 떨어지므로 18은 하샤드 수입니다. 자연수 x를 입력받아 x가 하샤드 수인지 아닌지 검사하는 함수, solution을 완성해주세요. #include #include using namespace std; bool solution(int x) { bool answer = true; string n = to_string(x); int a = 0; for(int i = 0; i < n.size();i++){ a += (n[i] - '0'); } x % a == 0 ? answer = true : answer = false; return answer; } 정수들의 합의 .. 2023. 8. 21.
프로그래머스 Level1. 정수 내림차순으로 배치하기 #include #include #include using namespace std; long long solution(long long n) { long long answer = 0; string num = to_string(n); sort(num.begin(), num.end()); reverse(num.begin(), num.end()); for(int i = num.size(); i > 0; i--){ answer = stoi(num); } return answer; } 계속 풀다가 첫번째로 통과되었지만 core dump가 발생해 다시 수정해야했던 답안 sort 사용해서 end() 부터 begin()까지 넣으려 했지만 여기서 core dump가 발생해 저렇게 작업해준 것이다. 새로운 함수를 찾아야.. 2023. 8. 20.
프로그래머스 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.
728x90
반응형