Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[스택, 큐, 덱] 9월 15일 #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 9월 9일 - 스택, 큐, 덱/11866.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <queue>

using namespace std;

int main()
{
queue<int> q;
int N, K;

cin >> N >> K; //입력받기

for (int i = 1; i <= N; i++)
q.push(i);

cout << "<";

while (!q.empty())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연산을 하는 부분은 함수화해도 좋을 것 같아요. 함수에서 vector에 원소를 순서대로 저장하고
vector를 main에서 받아서 한 번에 출력하면 어떨까요?

{
for (int i = 1; i < K; i++)
{
q.push(q.front());
q.pop();
}

cout << q.front();
q.pop();

if(!q.empty())
cout << ", ";
Comment on lines +29 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< , >를 사이사이에 출력해야 해서 출력 조건이 조금 복잡하게 들어갔네요..! 앞서 말씀드린 것처럼 vector에 저장한 후에 출력하면 좀 더 코드가 깔끔해질 것 같아요!!


}
cout << ">" << '\n';

return 0;
}
60 changes: 60 additions & 0 deletions 9월 9일 - 스택, 큐, 덱/18115.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <deque>

using namespace std;

/*
hint: 결과에서부터 역순으로 생각하기!
*/


int main() {

int n; //N장
cin >> n; //입력받기

vector<int> arr(n); //수열 A
deque<int> deq;

int card = 1;

//입력받기
for (int i = 0; i < n; i++) {
cin >> arr[i];
}


//i번째로 카드를 내려놓을 때 x번 기술 사용. 기술 순서 역순으로 생각하기!
//x=1: 맨 뒤에 push
//x=2: 맨 뒤에서 두번째에 push
//x=3: 맨 앞에 push

for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 1) {
//맨 뒤에 push
deq.push_back(card);
}
else if (arr[i] == 2) {
int end = deq.back();
deq.pop_back();
//맨 뒤에서 두번째에 push
deq.push_back(card);
deq.push_back(end);
}
else if (arr[i] == 3) {
//맨 앞에 push
deq.push_front(card);
}

//순서대로 1,2, ..., N.
card++;
}
Comment on lines +33 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2. 해당 부분은 문제 풀이의 핵심 부분이니 함수화 하시는 것을 권장드립니다!
참고로 코딩테스트에선 대부분 main이 제공되지 않고 solution 함수만 제공되어 정답을 리턴하는 형태이니 함수화에 익숙해지시면 좋을 것 같습니다!


//출력
for (int i = n-1; i >=0; i--) {
cout << deq[i] << ' ';
}
cout << '\n';

}