当前位置:网站首页>Divide candy (circular queue)
Divide candy (circular queue)
2022-07-03 08:51:00 【Cap07】
#include<iostream>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef int Status;
int all = 0;
// First element :Q->front->next->data
// The last element :Q->rear->data
typedef struct Node { // Store data field and pointer field ( Auxiliary )
ElemType data;
struct Node* next;
}QNode, * QueuePtr;
typedef struct { // Pointer to the beginning and end of the queue ( Lord )
QueuePtr front;
QueuePtr rear;
}LinkQ, * LinkQueue;
LinkQueue InitQueue() { // Construct an empty queue
LinkQueue Q;
Q = (LinkQueue)malloc(sizeof(LinkQ));
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
Q->front->next = NULL;
return Q;
}
LinkQueue link_Queue(LinkQueue Q) { // Connect into a circular queue
Q->rear->next = Q->front->next;
return Q;
}
void PrintQueue(LinkQueue Q) { // Output queue
while (Q->front->next) {
printf("%d ", Q->front->next->data);
Q->front = Q->front->next;
}
}
void PrintQueueL(LinkQueue Q, int length) { // Output data of the specified length in the queue
for (int i = 1; i <= length; i++) {
cout << Q->front->next->data << " ";
Q->front = Q->front->next;
}
}
void DestroyQueue(LinkQueue Q) { // Destroy queue
while (Q->front) {
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
}
void EnQueue(LinkQueue Q, int e) { // Insert elements e End of team element
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
}
Status DeQueue(LinkQueue Q) { // Return to team leader element
if (Q->front == Q->rear) return 0;
int e = Q->front->next->data;
return e;
}
LinkQueue LinkHalf(LinkQueue Q,int length) { // Pass half to the next
int a[10000] = {};
for (int i = 1; i <= length; i++) {
a[i] = Q->front->next->data/2;
Q->front = Q->front->next;
}
for (int i = 1; i <= length; i++) {
a[i+length] = Q->front->next->data/2;
Q->front = Q->front->next;
}
for (int i = 1; i <= length; i++) {
Q->front->next->next->data += a[i] - a[i + 1];
Q->front = Q->front->next;
}
return Q;
}
int panduanQueue(LinkQueue Q, int length) { // Determine whether all elements are equal
for (int i = 1; i < length; i++) {
if (Q->front->next->data != Q->front->next->next->data) {
return 0;
}
Q->front = Q->front->next;
}
return 1;
}
LinkQueue jishujiayi(LinkQueue Q, int length) {
for (int i = 1; i <= length; i++) {
if (Q->front->next->data % 2 == 1) {
Q->front->next->data++;
all++;
}
Q->front = Q->front->next;
}
return Q;
}
int main() {
LinkQueue Q;
Q = InitQueue();
int N,temp;
cin >> N;
int i, j;
for (i = 1; i <= N; i++) {
cin >> temp;
EnQueue(Q, temp);
}
Q = link_Queue(Q);
while (!panduanQueue(Q, N)) {
Q=LinkHalf(Q, N);
//cout << " result 1 yes " << endl;
//PrintQueueL(Q, N);
Q = jishujiayi(Q, N);
//cout << " The result is " << endl;
//PrintQueueL(Q, N);
}
cout << all << endl;
return 0;
}
test result :
边栏推荐
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Osganimation library parsing
- 单调栈-42. 接雨水
- UE4 source code reading_ Bone model and animation system_ Animation process
- Development experience and experience
- Really explain the five data structures of redis
- MySQL index types B-tree and hash
- Final review of Database Principles
- 【Rust 笔记】08-枚举与模式
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
猜你喜欢
Vscode, idea, VIM development tool shortcut keys
[concurrent programming] concurrent tool class of thread
Graphics_ Games101/202 learning notes
[concurrent programming] explicit lock and AQS
Notes and bugs generated during the use of h:i:s and y-m-d
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
[redis] redis persistent RDB vs AOF (source code)
Dom4j traverses and updates XML
Monotonic stack -503 Next bigger Element II
JS ternary operator - learning notes (with cases)
随机推荐
【Rust 笔记】07-结构体
如何应对数仓资源不足导致的核心任务延迟
Deeply understand the underlying data structure of MySQL index
[rust notes] 05 error handling
Intersectionpicker in osgearth
[concurrent programming] working mechanism and type of thread pool
Constraintlayout's constraintset dynamically modifies constraints
Get the link behind? Parameter value after question mark
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
记忆化搜索 AcWing 901. 滑雪
Unity notes 1
Downward compatibility and upward compatibility
MySQL containerization (1) docker installation MySQL
Message pack in C deserializes array objects
Unity editor expansion - draw lines
[rust notes] 08 enumeration and mode
Monotonic stack -503 Next bigger Element II
Development experience and experience
OpenGL learning notes
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis