//큐
#include <iostream>
#include <string>
using namespace std;
#define MAX_QUEUE_SIZE 100
class CircularQueue {
protected:
int front;
int rear;
int data[MAX_QUEUE_SIZE];
public:
CircularQueue() { front = rear = 0; }
bool isEmpty() { return front == rear; }
bool isFull() { return (rear + 1) % MAX_QUEUE_SIZE == front; }
int empty() {
if (front == rear) return 1;
else return 0;
}
void enqueue(int val);
int dequeue();
int peek();
int last_peek();
void display();
int size();
};
// 큐에 삽입
void CircularQueue::enqueue(int val)
{
if (isFull()) cout << "error: 큐가 포화상태입니다" << endl;
else {
rear = (rear + 1) % MAX_QUEUE_SIZE;
data[rear] = val;
}
}
// 큐에서 첫 번째 요소 삭제 & 반환
int CircularQueue::dequeue()
{
if (isEmpty()) return -1;
else {
front = (front + 1) % MAX_QUEUE_SIZE;
return data[front];
}
}
// 큐에서 첫 번째 요소 반환 (첫번째 요소 삭제하지 않음)
int CircularQueue::peek()
{
if (isEmpty()) return -1;
else
return data[(front + 1) % MAX_QUEUE_SIZE];
}
// 큐에서 마지막 번째 요소 반환 (마지막 요소 삭제하지 않음)
int CircularQueue::last_peek()
{
if (isEmpty()) return -1;
else
return data[(rear) % MAX_QUEUE_SIZE];
}
// 큐의 모든 요소들을 순서대로 출력
void CircularQueue::display()
{
printf("큐 내용 : ");
int maxi = (front < rear) ? rear : rear + MAX_QUEUE_SIZE;
for (int i = front + 1; i <= maxi; i++)
cout << data[i % MAX_QUEUE_SIZE] << " ";
cout << endl;
}
// 큐의 크기 반환
int CircularQueue::size()
{
return (rear - front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
int main() {
CircularQueue q;
int n, num, que[MAX_QUEUE_SIZE];
int cnt = 0;
string command;
// 입력
cin >> n;
// n개의 명령어 처리
for (int i = 0; i < n; i++) {
cin >> command; // 연산자 입력
if (command == "push") { cin >> num; q.enqueue(num); } // 각 명령어 처리
else if (command == "pop") que[cnt++] = q.dequeue();
else if (command == "size") que[cnt++] = q.size();
else if (command == "empty") que[cnt++] = q.empty();
else if (command == "front") que[cnt++] = q.peek();
else if (command == "back") que[cnt++] = q.last_peek();
}
// 출력
for (int i = 0; i < cnt; i++) {
cout << que[i] << endl;
}
return 0;
}
//순열
#include <iostream>
#include <string>
using namespace std;
#define MAX_QUEUE_SIZE 100
class CircularQueue {
protected:
int front;
int rear;
int data[MAX_QUEUE_SIZE];
public:
CircularQueue() { front = rear = 0; }
bool isEmpty() { return front == rear; }
bool isFull() { return (rear + 1) % MAX_QUEUE_SIZE == front; }
int empty() {
if (front == rear) return 1;
else return 0;
}
void enqueue(int val);
int dequeue();
int peek();
int last_peek();
void display();
int size();
};
// 큐에 삽입
void CircularQueue::enqueue(int val)
{
if (isFull()) { cout << "error: 큐가 포화상태입니다" << endl; exit(-1); }
else {
rear = (rear + 1) % MAX_QUEUE_SIZE;
data[rear] = val;
}
}
// 큐에서 첫 번째 요소 삭제 & 반환
int CircularQueue::dequeue()
{
if (isEmpty()) { cout << " Error: 큐가 공백상태입니다"; exit(-1); }
else {
front = (front + 1) % MAX_QUEUE_SIZE;
return data[front];
}
}
// 큐에서 첫 번째 요소 반환 (첫번째 요소 삭제하지 않음)
int CircularQueue::peek()
{
if (isEmpty()) { cout <<" Error: 큐가 공백상태입니다\n"; return -1; }
else
return data[(front + 1) % MAX_QUEUE_SIZE];
}
// 큐에서 마지막 번째 요소 반환 (마지막 요소 삭제하지 않음)
int CircularQueue::last_peek()
{
if (isEmpty()) return -1;
else
return data[(rear) % MAX_QUEUE_SIZE];
}
// 큐의 모든 요소들을 순서대로 출력
void CircularQueue::display()
{
printf("큐 내용 : ");
int maxi = (front < rear) ? rear : rear + MAX_QUEUE_SIZE;
for (int i = front + 1; i <= maxi; i++)
cout << data[i % MAX_QUEUE_SIZE] << " ";
cout << endl;
}
// 큐의 크기 반환
int CircularQueue::size()
{
return (rear - front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
void main() {
CircularQueue q;
int n, k;
int num[MAX_QUEUE_SIZE];
// 입력
cin >> n >> k;
for (int i = 1; i <= n; i++) {
q.enqueue(i);
}
// 큐 연산 처리
int cnt = 0;
while (q.size() > 1) { // 1명이 남을 때까지 반복
for (int i = 1; i < k; i++) q.enqueue(q.dequeue()); // k-1번째까지 유지(앞에 k-1번째 요소들을 삭제하고 다시 삽입)
num[cnt++] = q.peek();
q.dequeue(); // k번째 제거
if (q.size() == 1) break; // k번째 제거하고 1명만 남으면 중단
num[cnt++] = q.peek();
q.dequeue(); // k+1번재 제거
}
// 출력
for (int i = 0; i < cnt; i++) {
cout << num[i] << " ";
}
}
//기념품
#include <iostream>
#include <string>
using namespace std;
#define MAX_QUEUE_SIZE 100
class CircularQueue {
protected:
int front;
int rear;
int data[MAX_QUEUE_SIZE];
public:
CircularQueue() { front = rear = 0; }
bool isEmpty() { return front == rear; }
bool isFull() { return (rear + 1) % MAX_QUEUE_SIZE == front; }
int empty() {
if (front == rear) return 1;
else return 0;
}
void enqueue(int val);
int dequeue();
int peek();
int last_peek();
void display();
int size();
};
// 큐에 삽입
void CircularQueue::enqueue(int val)
{
if (isFull()) { cout << "error: 큐가 포화상태입니다" << endl; exit(-1); }
else {
rear = (rear + 1) % MAX_QUEUE_SIZE;
data[rear] = val;
}
}
// 큐에서 첫 번째 요소 삭제 & 반환
int CircularQueue::dequeue()
{
if (isEmpty()) { cout << " Error: 큐가 공백상태입니다"; exit(-1); }
else {
front = (front + 1) % MAX_QUEUE_SIZE;
return data[front];
}
}
// 큐에서 첫 번째 요소 반환 (첫번째 요소 삭제하지 않음)
int CircularQueue::peek()
{
if (isEmpty()) { cout <<" Error: 큐가 공백상태입니다\n"; return -1; }
else
return data[(front + 1) % MAX_QUEUE_SIZE];
}
// 큐에서 마지막 번째 요소 반환 (마지막 요소 삭제하지 않음)
int CircularQueue::last_peek()
{
if (isEmpty()) return -1;
else
return data[(rear) % MAX_QUEUE_SIZE];
}
// 큐의 모든 요소들을 순서대로 출력
void CircularQueue::display()
{
printf("큐 내용 : ");
int maxi = (front < rear) ? rear : rear + MAX_QUEUE_SIZE;
for (int i = front + 1; i <= maxi; i++)
cout << data[i % MAX_QUEUE_SIZE] << " ";
cout << endl;
}
// 큐의 크기 반환
int CircularQueue::size()
{
return (rear - front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
void main() {
CircularQueue q;
int n;
// 입력
cin >> n;
// 큐 연산
for (int i = 1; i <= n; i++) q.enqueue(i);
int t = 1;
while (q.size() > 1) { // 한 명이 남을 때까지 반복
for (int i = 1; i < (t * t + 1); i++) // (t*t+1)-1번째 사람까지 유지, 큐에서 삭제하여 다시 삽입
q.enqueue(q.dequeue());
q.dequeue(); // (t*t+1)번째 사람 삭제
t++;
}
// 출력
cout << q.peek();
}