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

边栏推荐
- Markdown learning
- [concurrent programming] Table hopping and blocking queue
- [rust notes] 12 closure
- cres
- [concurrent programming] thread foundation and sharing between threads
- Concurrent programming (VI) ABA problems and solutions under CAS
- Unity editor expansion - the framework and context of unity imgui
- JS ternary operator - learning notes (with cases)
- Development experience and experience
- php public private protected
猜你喜欢

Unity interactive water ripple post-treatment

Visual Studio (VS) shortcut keys

Es8 async and await learning notes

Alibaba canal actual combat

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

Graphics_ Games101/202 learning notes
![[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q

Constraintlayout's constraintset dynamically modifies constraints

高斯消元 AcWing 883. 高斯消元解线性方程组

了解小程序的笔记 2022/7/3
随机推荐
Unity learning notes
Solution of 300ms delay of mobile phone
Unity editor expansion - the design idea of imgui
Unity editor expansion - scrolling list
【Rust 笔记】13-迭代器(上)
Alibaba canaladmin deployment and canal cluster Ha Construction
OpenGL learning notes
求组合数 AcWing 886. 求组合数 II
redis集群系列四
Unity Editor Extension - event handling
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Really explain the five data structures of redis
DOM 渲染系统(render mount patch)响应式系统
Drawing maze EasyX library with recursive backtracking method
[concurrent programming] consistency hash
【Rust 笔记】07-结构体
【Rust 笔记】09-特型与泛型
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Convert video to GIF
[concurrent programming] thread foundation and sharing between threads