-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uC2A4\uD0DD\uD050\uB371"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()) | ||
{ | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. < , >를 사이사이에 출력해야 해서 출력 조건이 조금 복잡하게 들어갔네요..! 앞서 말씀드린 것처럼 vector에 저장한 후에 출력하면 좀 더 코드가 깔끔해질 것 같아요!! |
||
|
||
} | ||
cout << ">" << '\n'; | ||
|
||
return 0; | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 해당 부분은 문제 풀이의 핵심 부분이니 함수화 하시는 것을 권장드립니다! |
||
|
||
//출력 | ||
for (int i = n-1; i >=0; i--) { | ||
cout << deq[i] << ' '; | ||
} | ||
cout << '\n'; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
연산을 하는 부분은 함수화해도 좋을 것 같아요. 함수에서 vector에 원소를 순서대로 저장하고
vector를 main에서 받아서 한 번에 출력하면 어떨까요?