-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCTDL083.cpp
66 lines (58 loc) · 1.16 KB
/
SCTDL083.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @file SCTDL083.cpp
* @author long ([email protected])
* @brief Reverse ordering of the first k elements of array
* @version 0.1
* @date 2023-04-09
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
void reverseQueueFirstKElements(int k, queue<int>& Queue)
{
if (Queue.empty() == true || k > Queue.size())
return;
if (k <= 0)
return;
stack<int> Stack;
for(int i=0; i < k; i++) {
Stack.push(Queue.front());
Queue.pop();
}
while(!Stack.empty())
{
Queue.push(Stack.top());
Stack.pop();
}
for(int i=0; i < Queue.size()-k; i++)
{
Queue.push(Queue.front());
Queue.pop();
}
}
void Print(queue<int>& Queue)
{
while (!Queue.empty()) {
cout << Queue.front() << " ";
Queue.pop();
}
}
int main() {
int t;
cin >> t;
while(t--) {
queue<int> queue;
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
queue.push(tmp);
}
reverseQueueFirstKElements(k, queue);
Print(queue);
}
return 0;
}