프로그래머스 - 코딩 기초 트레이닝 캘린더 Day 5
1. 코드 처리하기
https://school.programmers.co.kr/learn/courses/30/lessons/181932
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
string solution(string code) {
string answer = "";
int mode = 0;
for (int i = 0; i < code.size(); ++i)
{
if (code[i] == '1')
mode = !mode;
else
{
if (mode == 0 && i % 2 == 0)
answer += code[i];
else if (mode == 1 && i % 2 == 1)
answer += code[i];
}
}
if (answer.length() == 0)
return "EMPTY";
return answer;
}
조건을 일일이 구현해주면 되는 간단한 문제이다.
2. 등차수열의 특정한 항만 더하기
https://school.programmers.co.kr/learn/courses/30/lessons/181931
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int solution(int a, int d, vector<bool> included) {
int answer = 0;
for (int i = 0; i < included.size(); ++i)
{
if (included[i] == true)
answer += (a + (d * i));
}
return answer;
}
결국 answer는 각 인덱스 i마다 a + (d x i) 에 해당하는 값을 더해주면 된다.
3. 주사위 게임 2
https://school.programmers.co.kr/learn/courses/30/lessons/181930
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int solution(int a, int b, int c) {
int answer = 0;
if (a == b && b == c)
answer = (a + b + c) * ((a * a) + (b * b) + (c * c)) * ((a * a * a) + (b * b * b) + (c * c * c));
else if (a != b && b != c && c != a)
answer = a + b + c;
else
answer = (a + b + c) * ((a * a) + (b * b) + (c * c));
return answer;
}
코드를 작성하기 귀찮을 뿐, 쉬운 문제이다.
4. 원소들의 곱과 합
https://school.programmers.co.kr/learn/courses/30/lessons/181929
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> num_list) {
int answer = 0;
// 모든 원소들의 곱
int a = 1;
for (int i = 0; i < num_list.size(); ++i)
a *= num_list[i];
// 모든 원소들의 합의 제곱
int b = 0;
for (int i = 0; i < num_list.size(); ++i)
b += num_list[i];
b *= b;
if (a < b)
answer = 1;
else
answer = 0;
return answer;
}
5. 이어 붙인 수
https://school.programmers.co.kr/learn/courses/30/lessons/181928
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> num_list) {
int answer = 0;
int onlyEvenNum = 0; // 짝수만 이어 붙인 수
int onlyOddNum = 0; // 홀수만 이어 붙인 수
for (int i = 0; i < num_list.size(); ++i)
{
int num = num_list[i];
if (num % 2 == 0)
onlyEvenNum = (onlyEvenNum * 10) + num;
else
onlyOddNum = (onlyOddNum * 10) + num;
}
answer = onlyEvenNum + onlyOddNum;
return answer;
}
짝수만 이어 붙인 수와 홀수만 이어 붙인 수를 string을 이용해서 구할 수도 있지만, 그럴 경우 변수 이름 짓기가 난감해져서 string을 이용하지 않았다.