일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- 2023년
- 7월
- 기초
- 다이나믹 프로그래밍
- 개인 프로젝트
- 입문
- c++
- C/C++
- 단계별로 풀어보기
- 5월
- 유니티
- 자료 구조
- 6월
- 백준
- 골드메탈
- 1월
- 4월
- 2024년
- 게임 엔진 공부
- 2022년
- 수학
- 3월
- 프로그래머스
- 코딩 기초 트레이닝
- 유니티 심화과정
- todolist
- 코딩 테스트
- 개인 프로젝트 - 런앤건
- 2월
- 2025년
- Today
- Total
기록 보관소
[프로그래머스] 코딩 기초 트레이닝 PART 9(C++) 본문
프로그래머스 코딩 테스트 연습 : 코딩 기초 트레이닝(C++)
https://school.programmers.co.kr/learn/challenges?order=recent&languages=cpp&partIds=44139&page=3
코딩테스트 연습 | 프로그래머스 스쿨
개발자 취업의 필수 관문 코딩테스트를 철저하게 연습하고 대비할 수 있는 문제를 총망라! 프로그래머스에서 선발한 문제로 유형을 파악하고 실력을 업그레이드해 보세요!
school.programmers.co.kr
프로그래머스 코딩 기초 트레이닝 : 배열 만들기 5
https://school.programmers.co.kr/learn/courses/30/lessons/181912
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
문자열 배열 intStrs와 정수 k, s, l가 주어집니다.
intStrs의 원소는 숫자로 이루어져 있습니다.
배열 intStrs의 각 원소마다 s번 인덱스에서 시작하는 길이 l짜리 부분 문자열을 잘라내 정수로 변환합니다.제한사항
- 0 ≤ s < 100
- 1 ≤ l ≤ 8
- 10^(l - 1) ≤ k < 10^l
- 1 ≤ intStrs의 길이 ≤ 10,000
- s + l ≤ intStrs의 원소의 길이 ≤ 120
입출력 예


코드
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> intStrs, int k, int s, int l) {
vector<int> answer;
for (auto intStr : intStrs) {
string temp = "";
for (int i = s; i < s+l; i++)
temp += intStr[i];
if (stoi(temp) > k)
answer.push_back(stoi(temp));
}
return answer;
}
// 다른 사람의 풀이 1 : substr을 사용하는 방법 1
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> intStrs, int k, int s, int l) {
vector<int> answer;
for(int i=0;i<intStrs.size();i++){
string a = intStrs[i].substr(s,l);
int b=stoi(a);
if(b>k) answer.push_back(b);
}
return answer;
}
// 다른 사람의 풀이 2 : substr을 사용하는 방법 2
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> intStrs, int k, int s, int l) {
vector<int> answer;
for(const auto& str : intStrs)
{
int tmp = stoi(str.substr(s,l));
if(tmp > k)
{
answer.emplace_back(tmp);
}
}
return answer;
}
결과

프로그래머스 코딩 기초 트레이닝 : 부분 문자열 이어 붙여 문자열 만들기
https://school.programmers.co.kr/learn/courses/30/lessons/181911
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
제한사항
- 1 ≤ my_strings의 길이 = parts의 길이 ≤ 100
- 1 ≤ my_strings의 원소의 길이 ≤ 100
- parts[i]를 [s, e]라 할 때, 다음을 만족합니다.
- 0 ≤ s ≤ e < my_strings[i]의 길이
입출력 예


코드
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> my_strings, vector<vector<int>> parts) {
string answer = "";
int count = 0;
for (auto part : parts) {
for (int i = part[0]; i <= part[1]; i++)
answer += my_strings[count][i];
count++;
}
return answer;
}
// 다른 사람의 풀이 1 : 참조(auto&)와 substr을 사용하는 방법
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> my_strings, vector<vector<int>> parts) {
string answer = "";
for (int i=0; i<parts.size(); ++i) {
auto& p = parts[i];
answer += my_strings[i].substr(p[0], p[1]-p[0]+1);
}
return answer;
}
// 다른 사람의 풀이 2 : substr만 사용하는 방법
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> my_strings, vector<vector<int>> parts) {
string answer = "";
for(int t = 0; t < my_strings.size(); ++t)
{
answer += my_strings[t].substr(parts[t][0],parts[t][1]-parts[t][0]+1);
}
return answer;
}
결과

프로그래머스 코딩 기초 트레이닝 : 문자열의 뒤의 n글자
https://school.programmers.co.kr/learn/courses/30/lessons/181910
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
제한사항
- my_string은 숫자와 알파벳으로 이루어져 있습니다.
- 1 ≤ my_string의 길이 ≤ 1,000
- 1 ≤ n ≤ my_string의 길이
입출력 예


코드
#include <string>
#include <vector>
using namespace std;
string solution(string my_string, int n) {
string answer = "";
for (int i = my_string.size() - n; i < my_string.size(); i++)
answer += my_string[i];
return answer;
}
// 다른 사람의 풀이 : substr을 사용하는 방법
#include <string>
#include <vector>
using namespace std;
string solution(string my_string, int n) {
return my_string.substr(my_string.size()-n);
}
결과

프로그래머스 코딩 기초 트레이닝 : 접미사 배열
https://school.programmers.co.kr/learn/courses/30/lessons/181909
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
제한사항
- my_string은 알파벳 소문자로만 이루어져 있습니다.
- 1 ≤ my_string의 길이 ≤ 100
입출력 예


코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> solution(string my_string) {
vector<string> answer;
for (int i = 0; i < my_string.size(); i++)
answer.push_back(my_string.substr(i));
sort(answer.begin(), answer.end());
return answer;
}
결과

프로그래머스 코딩 기초 트레이닝 : 접미사인지 확인하기
https://school.programmers.co.kr/learn/courses/30/lessons/181908
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
문자열 my_string과 is_suffix가 주어질 때, is_suffix가 my_string의 접미사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요.
제한사항
- 1 ≤ my_string의 길이 ≤ 100
- 1 ≤ is_suffix의 길이 ≤ 100
- my_string과 is_suffix는 영소문자로만 이루어져 있습니다.
입출력 예


코드
#include <string>
#include <vector>
using namespace std;
int solution(string my_string, string is_suffix) {
return ((my_string.size() >= is_suffix.size()) && (my_string.substr(my_string.size() - is_suffix.size(), my_string.size()) == is_suffix));
}
// 다른 사람의 풀이 : 내 풀이와 동일하나 가독성이 좋음(...)
#include <string>
#include <vector>
using namespace std;
int solution(string my_string, string is_suffix) {
if(my_string.size() < is_suffix.size())
{
return 0;
}
return my_string.substr(my_string.size() - is_suffix.size()) == is_suffix;
}
결과

여담
이번 문제들은 대부분 문제를 다 읽고 거의 10분 내로 다 풀었다.
다만 substr 함수를 사용하지 않고 풀었던 문제가 더 많아서, 비교적 코드가 다른 사람의 풀이에 비해 조금 길어지고 복잡한게 많았던 것 같다.
그래도 2 문제는 substr을 써서 해결했다ㅎㅎ...
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 코딩 기초 트레이닝 PART 11(C++) (0) | 2024.01.18 |
---|---|
[프로그래머스] 코딩 기초 트레이닝 PART 10(C++) (0) | 2024.01.17 |
[프로그래머스] 코딩 기초 트레이닝 PART 8(C++) (2) | 2024.01.15 |
[프로그래머스] 코딩 기초 트레이닝 PART 7(C++) (2) | 2024.01.11 |
[프로그래머스] 코딩 기초 트레이닝 PART 6(C++) (2) | 2024.01.10 |