[프로그래머스][Level1] 문자열 다루기 기본 c++
·
카테고리 없음
https://programmers.co.kr/learn/courses/30/lessons/12918 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 여기서 실수할 수 있는 점은 숫자는 0부터 시작한다는 것이다! 1부터 시작하지 않도록 조심해야된다! 문자열 안에 있는 숫자들을 카운트 해주고, 문자열의 길이와 숫자 카운트한 것과 같으면 answer을 true로 바꿔준다. #include #include using namespace std; bool solution(string s) { bool answer = false; if(s.size() == ..
[프로그래머스][Level1] 문자열 내림차순으로 배치하기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12917 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 문자열도 char형이 여러개 이어진 것이기 때문에 sort를 할 수 있다. rbegin()과 rend()는 내림차순을 한다는 것이다. 풀이1 #include #include #include using namespace std; string solution(string s) { string answer = ""; sort(s.rbegin(), s.rend()); answer = s; return ans..
[프로그래머스][Level1] 문자열 내 p와 y의 개수 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12916 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 단순히 문자열 개수 세는 문제이다. #include #include using namespace std; bool solution(string s) { bool answer = true; int pcnt = 0, ycnt = 0; for(int i = 0; i < s.size(); i++){ if(s[i] == 'p' || s[i] == 'P') pcnt++; else if(s[i] == 'y' ||..
[프로그래머스][Level1] 두 정수 사이의 합 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12912 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 두 정수 사이의 합만 구하면 되는 단순한 문제이다. 만약 a보다 b가 크게 되면 swap함수로 두 값을 바꿔주면 된다. 풀이1 #include #include using namespace std; long long solution(int a, int b) { long long answer = 0; if(a > b) swap(a, b); for(int i = a; i b) { int temp = a; ..
[프로그래머스][Level1] 두 개 뽑아서 더하기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/68644 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 이중 for문을 돌려 answer 벡터에 합들을 다 넣어준다. 오름차순으로 정렬하고 중복인 숫자는 erase함수로 제거해준다. 풀이1 #include #include #include using namespace std; vector solution(vector numbers) { vector answer; for(int i = 0; i < numbers.size() - 1; i++){ for(int ..
[프로그래머스][Level1] 나머지가 1이 되는 수 찾기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/87389 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 answer을 1로 설정해주고 나머지가 1이 아니라면 answer을 + 1씩 해준다. 만약 나머지가 1이라면 무한 반복문을 탈출해준다. #include #include using namespace std; int solution(int n) { int answer = 1; while(1){ if(n % answer == 1) break; answer++; } return answer; }
[프로그래머스][Level1] 나누어 떨어지는 숫자 배열 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12903 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 나누어 떨어진다면 answer 벡터에 넣고, 오름차순으로 정렬해주면 된다. 풀이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.pus..
[프로그래머스][Level1] 같은 숫자는 싫어 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12906 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 중복을 제거하려면 벡터의 erase함수를 쓰면 된다. 풀이1 #include #include #include using namespace std; vector solution(vector arr) { vector answer; arr.erase(unique(arr.begin(), arr.end()), arr.end()); for(int i = 0; i < arr.size(); i++){ answer...
[프로그래머스][Level1] 가운데 글자 가져오기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12903 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 만약 s의 길이가 홀수라면 s.size() / 2 부분만 반환하면 되지만, 짝수라면 s.size() / 2 - 1인 부분도 반환해야된다. #include #include using namespace std; string solution(string s) { string answer = ""; int idx = s.size() / 2; if(s.size() % 2 == 0){ answer += s[id..
[프로그래머스][Level1] 3진법 뒤집기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/68935 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 3진법 상에서 앞뒤로 다시 뒤집어야 하니 reverse함수는 필요 없다. 뒷 자리수부터 3의 0 제곱, 3의 1 제곱,... 3의 n제곱을 곱해주면 3진법에서 10진법으로 바꿀 수 있다. 풀이1 #include #include #include using namespace std; string Num(int n){ string str = ""; while(n != 0){ str += to_string(n % 3..