프로그래머스/Level 1 41

[프로그래머스/C++] Level 1 자연수 뒤집어 배열로 만들기

문제 https://programmers.co.kr/learn/courses/30/lessons/12932 코딩테스트 연습 - 자연수 뒤집어 배열로 만들기 자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다. 제한 조건 n은 10,000,000,000이하인 자연수입니다. 입출력 예 n return 12345 programmers.co.kr 풀이 코드 #include #include using namespace std; vector solution(long long n) { vector answer; while(n != 0){ answer.push_back(n%10); n /= 10; } return answer; } ..

[프로그래머스/C++] Level 1 이상한 문자 만들기

문제 https://programmers.co.kr/learn/courses/30/lessons/12931 코딩테스트 연습 - 자릿수 더하기 자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요. 예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다. 제한사항 N의 범위 : 100,000,000 이하의 자연수 입출 programmers.co.kr 풀이 answer에 각 자리 수의 숫자만큼 더해줌 코드 #include using namespace std; int solution(int n) { int answer = 0; while(n != 0){ answer += n%10; n /= 10; } return answer; } ..

[프로그래머스/C++] Level 1 이상한 문자 만들기

문제 https://programmers.co.kr/learn/courses/30/lessons/12930 코딩테스트 연습 - 이상한 문자 만들기 문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다. 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 programmers.co.kr 풀이 문자열의 기준이 아니라 단어가 기준이기 때문에 위치 판별을 위한 count 변수 선언 코드 #include #include using namespace std; string solution(string s) { string answer = ""; int count = 0; for(int i=0;i

[프로그래머스/C++] Level 1 문자열을 정수로 바꾸기

문제 https://programmers.co.kr/learn/courses/30/lessons/12925 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 코드 #include #include using namespace std; int solution(string s) { int answer = 0; answer = stoi(s); return answer; } 시행착오

[프로그래머스/C++] Level 1 수박수박수박수박수박수?

문제 https://programmers.co.kr/learn/courses/30/lessons/12922 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 코드 #include #include using namespace std; string solution(int n) { string answer = ""; for(int i=0;i

[프로그래머스/C++] Level 1 문자열 다루기 기본

문제 https://programmers.co.kr/learn/courses/30/lessons/12918 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 문자의 길이가 4, 6인 경우만 검사 . . 아니면 false 문자의 길이만큼 for문을 돌리고, 0과 9 사이의 숫자가 아닌 경우 answer = false 후 for문 종료 코드 #include #include #include using namespace std; bool solution(string s) { bool answer = true; if(s.length() == 4 || s.leng..

[프로그래머스/C++] Level 1 문자열 내림차순으로 배치하기

문제 https://programmers.co.kr/learn/courses/30/lessons/12917 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 sort를 이용해서 내림차순으로 정렬해주면 됨 코드 #include #include #include using namespace std; string solution(string s) { string answer = ""; vector ans; for(int i=0;i

[프로그래머스/C++] Level 1 두 정수 사이의 합

문제 https://programmers.co.kr/learn/courses/30/lessons/12912 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 a가 b보다 클 때, 값을 바꿔줌 for문을 이용해 a부터 b까지의 합을 더해줌 a=b일 때는 고려하지 않아도 됨 코드 #include #include using namespace std; long long solution(int a, int b) { long long answer = 0; int temp = 0; if(a > b){ swap(a,b); } for(int i = a; i

[프로그래머스/C++] Level 1 가운데 글자 가져오기

문제 https://programmers.co.kr/learn/courses/30/lessons/12903 풀이 s의 길이가 짝수인지 홀수인지 먼저 판별 짝수라면 가운데 2글자 출력, 홀수라면 가운데 글자 출력 코드 #include #include using namespace std; string solution(string s) { string answer = ""; if(s.size() % 2 == 0){ answer = s[s.size()/2 - 1]; answer += s[s.size()/2]; } else{ answer = s[s.size()/2]; } return answer; } #include #include using namespace std; string solution(string s..