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

边栏推荐
- Convert video to GIF
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- Notes and bugs generated during the use of h:i:s and y-m-d
- Markdown learning
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Unity editor expansion - controls, layouts
- Unity learning notes
- Message queue for interprocess communication
- Drawing maze EasyX library with recursive backtracking method
猜你喜欢

Dom4j遍历和更新XML

Dom4j traverses and updates XML

Binary to decimal, decimal to binary

Servlet的生命周期

Final review of Database Principles

Markdown learning

Try to reprint an article about CSDN reprint
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

JS ternary operator - learning notes (with cases)
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
随机推荐
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
TP5 multi condition sorting
Gradle's method of dynamically modifying APK package name
file_ put_ contents
JS non Boolean operation - learning notes
Apache startup failed phpstudy Apache startup failed
Solution of 300ms delay of mobile phone
Dealing with duplicate data in Excel with xlwings
Try to reprint an article about CSDN reprint
Life cycle of Servlet
Installation of PHP FPM software +openresty cache construction
Final review of Database Principles
Baidu editor ueeditor changes style
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
记忆化搜索 AcWing 901. 滑雪
[rust notes] 09- special types and generics
Cesium for unreal quick start - simple scenario configuration
Graphics_ Learnopongl learning notes
Deeply understand the underlying data structure of MySQL index
如何应对数仓资源不足导致的核心任务延迟