[프로그래머스][Level1] 짝수와 홀수 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12937 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 짝수와 홀수 구하는 기본 문제이다. #include #include using namespace std; string solution(int num) { string answer = ""; if(num % 2 == 0) answer = "Even"; else if(num % 2 == 1) answer = "Odd"; return answer; }
[프로그래머스][Level1] 직사각형 별찍기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12969 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 가장 기본적인 별 찍기 문제이다. #include using namespace std; int main(void) { int n, m; cin >> n >> m; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cout
[프로그래머스][Level1] 제일 작은 수 제거하기 c++
·
🍞 Problem Solving/Programmers
https://school.programmers.co.kr/learn/courses/30/lessons/12935 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 arr.size()가 1인 배열에서 가장 작은 수를 지우면 arr.size()가 0이 되므로 처음부터 1인 경우 -1을 반환해준다. arr 배열에서 가장 작은 값을 res에 담고 res값이 아닌 것들만 answer 벡터에 넣어준다. 만약 sort를 하고 arr[0]이 아닌 것만 담아준다면 답도 정렬된 채로 나오니 불가능하다. 풀이1 #include #include #include usin..
[프로그래머스][Level1] 정수 제곱근 판별 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12934 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 n을 제곱근 한 것을 long long형 s에 담아준다. 안 그러면 채점에서 몇 개 틀린 것이 나온다...! n이 long long이어도 sqrt(n)을 하게 되면 int가 될 수도 있기 때문에 int * int = int이므로 처음부터 long long 변수에 담아줘야 한다. #include #include #include using namespace std; long long solution(lo..
[프로그래머스][Level1] 정수 내림차순으로 배치하기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12933 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 long long int형인 n을 string형으로 바꿔준다. string으로 바뀌면 내림차순으로 정렬을 해준다. answer은 long long 형태이므로 stoll함수를 써줘서 string을 long long으로 바꿔준다. 풀이1 #include #include #include using namespace std; long long solution(long long n) { long long an..
[프로그래머스][Level1] 자연수 뒤집어 배열로 만들기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12932 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 풀이1 자연수 n을 문자열로 바꾼 뒤, reverse함수를 실행시켜 문자열을 뒤집어준다. 그리고 char -> int 하기 위해 '0'을 빼주며 answer에 넣는다. #include #include #include using namespace std; vector solution(long long n) { vector answer; string str = to_string(n); reverse(st..
[프로그래머스][Level1] 자릿수 더하기 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12931 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 자연수 n을 string으로 바꾸고 각 자리수에 '0'을 빼줘서 문자를 숫자로 만든다. 숫자로 만든 것들을 반복문을 돌려 합을 구해준다. #include using namespace std; int solution(int n) { int answer = 0; string str = to_string(n); for(int i = 0; i < str.size(); i++){ answer += str[i]..
[프로그래머스][Level1] 약수의 합 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12928 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 약수를 구하는 알고리즘의 기본 문제이다. #include #include using namespace std; int solution(int n) { int answer = 0; for(int i = 1; i
[프로그래머스][Level1] 약수의 개수와 덧셈 c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/77884 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 이중 for문을 돌려 i값을 1 ~ i로 나눈 값이 0이면 약수라는 뜻이다. cnt로 약수의 개수를 구해 개수가 짝수면 더해주고, 홀수면 빼주는 식으로 구현했다. #include #include using namespace std; int solution(int left, int right) { int answer = 0; for(int i = left; i
[프로그래머스][Level1] 수박수박수박수박수박수? c++
·
🍞 Problem Solving/Programmers
https://programmers.co.kr/learn/courses/30/lessons/12922 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이과정 n이 짝수이면 n / 2만큼 "수박"을 문자열에 더해주면 되고, 만약 홀수이면 n / 2만큼 더해준 문자열에 "수"만 더하면 된다. #include #include using namespace std; string solution(int n) { string answer = ""; int cnt = n / 2; while(cnt--) answer += "수박"; if(n % 2 == 1) answer..