当前位置:网站首页>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 :

边栏推荐
- How to deal with the core task delay caused by insufficient data warehouse resources
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
- producer consumer problem
- 【Rust笔记】02-所有权
- Sending and receiving of request parameters
- Baidu editor ueeditor changes style
- Parameters of convolutional neural network
- Graphics_ Games101/202 learning notes
- XPath实现XML文档的查询
- Allocation exception Servlet
猜你喜欢

Unity learning notes

请求参数的发送和接收

UE4 source code reading_ Mobile synchronization

Es8 async and await learning notes

Notes and bugs generated during the use of h:i:s and y-m-d

MySQL three logs

求组合数 AcWing 886. 求组合数 II

Unity Editor Extension - drag and drop

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
随机推荐
createjs easeljs
Parameters of convolutional neural network
Chocolate installation
Allocation exception Servlet
Sending and receiving of request parameters
请求参数的发送和接收
Unity editor expansion - the framework and context of unity imgui
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Dealing with duplicate data in Excel with xlwings
Explain sizeof, strlen, pointer, array and other combination questions in detail
单调栈-84. 柱状图中最大的矩形
Convert video to GIF
[RPC] RPC remote procedure call
Unity interactive water ripple post-treatment
求组合数 AcWing 886. 求组合数 II
[rust notes] 11 practical features
[rust notes] 02 ownership
Installation of PHP FPM software +openresty cache construction
Get the link behind? Parameter value after question mark