3.123

news/2024/10/22 8:44:24/

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
/* 定义循环队列结构体 */
typedef struct {
    int data[MAX_SIZE]; // 存储元素的数组
    int front, rear;    // 队头指针和队尾指针
    int count;          // 记录元素个数
} Queue;
/* 初始化队列 */
void initQueue(Queue *queue) {
    queue->front = queue->rear = 0;
    queue->count = 0;
}
/* 判断队列是否已满 */
int isFull(Queue *queue) {
    return queue->count == MAX_SIZE;
}
/* 判断队列是否为空 */
int isEmpty(Queue *queue) {
    return queue->count == 0;
}
/* 元素入队 */
void enqueue(Queue *queue, int value) {
    if (isFull(queue)) {
        printf("The queue is full!\n");
        return;
    }
    /* 在这里进行一次判断,防止把元素直接存入队列中 */
    if (value > MAX_SIZE) {
        printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
        return;
    }

    queue->data[queue->rear] = value;
    queue->rear = (queue->rear + 1) % MAX_SIZE;
    queue->count++;

    printf("队列元素为: %d\n", value);
}
/* 元素出队 */
int dequeue(Queue *queue) {
    if (isEmpty(queue)) {
        printf("The queue is empty!\n");
        return 0;
    }
    int value = queue->data[queue->front];
    queue->front = (queue->front + 1) % MAX_SIZE;
    queue->count--;
    printf("出队列元素为: %d\n", value);
    return value;
}

/* 打印队列 */
void printQueue(Queue *queue) {
    printf("队列为: ");
    int i = queue->front;
    while (i != queue->rear) {
        printf("%d ", queue->data[i]);
        i = (i + 1) % MAX_SIZE;
    }
    printf("\n");
}

/* 主函数 */
int main() {
    
    Queue queue;
    initQueue(&queue);
    
    /* 从键盘输入元素数据,一旦输入超限,把超出部分未入队的元素暂存起来,等前面的元素出队后再入队 */
    int n, value, tmp_n = 0, tmp_data[MAX_SIZE];

    printf("请输入队列中元素的个数为:");
    scanf("%d", &n);

    while (n > MAX_SIZE) {
        printf("输入数字大于容纳量,请重新输入元素个数:");
        scanf("%d", &n);
    }

    printf("请输入%d个元素:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &value);

        if (value > MAX_SIZE) {
            printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
            tmp_data[tmp_n] = value;
            tmp_n++;
        } else {
            enqueue(&queue, value);
        }
    }
    // 一直出队,直到队列为空
    while (!isEmpty(&queue)) {
        int value = dequeue(&queue);
        printQueue(&queue);

        // 如果有暂存的元素,在最后再入队
        while (tmp_n > 0) {
            tmp_n--;
            enqueue(&queue, tmp_data[tmp_n]);
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 4
/* 定义循环队列结构体 */
typedef struct {
    int data[MAX_SIZE]; // 存储元素的数组
    int front, rear;    // 队头指针和队尾指针
    int count;          // 记录元素个数
} Queue;

/* 初始化队列 */
void initQueue(Queue *queue) {
    queue->front = queue->rear = 0;
    queue->count = 0;
}

/* 判断队列是否已满 */
int isFull(Queue *queue) {
    return queue->count == MAX_SIZE;
}

/* 判断队列是否为空 */
int isEmpty(Queue *queue) {
    return queue->count == 0;
}

/* 元素入队 */
void enqueue(Queue *queue, int value) {
    if (isFull(queue)) {
        printf("The queue is full! The element cannot be added temporarily!\n");
        return;
    }

    /* 在这里进行一次判断,防止把元素直接存入队列中 */
    if (value > MAX_SIZE) {
        printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
        return;
    }

    queue->data[queue->rear] = value;
    queue->rear = (queue->rear + 1) % MAX_SIZE;
    queue->count++;

    printf("Enqueued element: %d\n", value);
}

/* 元素出队 */
int dequeue(Queue *queue) {
    if (isEmpty(queue)) {
        printf("The queue is empty!\n");
        exit(1);
    }

    int value = queue->data[queue->front];
    queue->front = (queue->front + 1) % MAX_SIZE;
    queue->count--;

    printf("Dequeued element: %d\n", value);

    return value;
}

/* 打印队列 */
void printQueue(Queue *queue) {
    printf("Queue: ");
    int i = queue->front;
    while (i != queue->rear) {
        printf("%d ", queue->data[i]);
        i = (i + 1) % MAX_SIZE;
    }
    printf("\n");
}

/* 主函数 */
int main() {
    
    Queue queue;
    initQueue(&queue);
    
    /* 从键盘输入元素数据,一旦输入超限,把超出部分未入队的元素暂存起来,等前面的元素出队后再入队 */
    int n, value, tmp_n = 0, tmp_data[MAX_SIZE];

    printf("Please input the number of elements:");
    scanf("%d", &n);

    while (n > MAX_SIZE) {
        printf("The number of elements cannot exceed the maximum size of the queue! Please enter again:");
        scanf("%d", &n);
    }

    printf("Please input %d elements:\n", n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &value);

        if (value > MAX_SIZE) {
            printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
            tmp_data[tmp_n] = value;
            tmp_n++;
        } else {
            enqueue(&queue, value);
        }
    }

    // 一直出队,直到队列为空
    while (!isEmpty(&queue)) {
        int value = dequeue(&queue);
        printQueue(&queue);

        // 如果有暂存的元素,在最后再入队
        while (tmp_n > 0) {
            tmp_n--;
            enqueue(&queue, tmp_data[tmp_n]);
        }
    }

    return 0;
}

#include <stdio.h>
#define MAX 100
#define FALSE 0
#define TRUE 1
//循环队列
typedef struct {
    int element[MAX];
    int front; //头指针
    int rear;  //尾指针
} SeqQueue;
//初始化循环队列
void InitQueue(SeqQueue *q) 

q->front = q->rear = 0; 
}
//入队
int EnterQueue(SeqQueue *q, int x) {
    if ((q->rear + 1) % MAX == q->front) {
        printf("---队列已满---");
        return FALSE;
    }
    q->element[q->rear] = x;
    q->rear = (q->rear + 1) % MAX;
    return TRUE;
}
//出队
int DeleteQueue(SeqQueue *q, int *x) {
    if (q->front == q->rear) {
        printf("---队列为空---");
        return FALSE;
    }
    *x = q->element[q->front];
    q->front = (q->front + 1) % MAX;
    return TRUE;
}
//取对头元素
int GetHead(SeqQueue *q, int *x) {
    if (q->front == q->rear)
        return FALSE;
    *x = q->element[q->front];
    return TRUE;
}
//判断队列是否为空
int IsEmpty(SeqQueue *q) {
    if (q->front == q->rear)
        return TRUE;
    else
        return FALSE;
}
//打印杨辉三角
void YangHuiTriangle(int N) {
    SeqQueue q;
    InitQueue(&q);
    int n, i, x, temp;
    EnterQueue(&q, 1); //第一行元素入队
    for (n = 2; n <= N; n++) {
        EnterQueue(&q, 1); //第n行第一个元素入队
        for (i = N; i >= n; i--) //给数字间加空格,打印出金字塔形状
            printf("   ");       // N为打印的行数,n为每行的元素个数
        for (i = 1; i <= n - 2; i++) { //利用队中第n-1行元素产生第n行的中间n-2个元素并入队
            DeleteQueue(&q, &temp);    //出队元素赋给temp
            printf("%6d", temp);       //打印第n-1行的元素
            GetHead(&q, &x);
            temp = temp + x;      //利用第n-1行元素产生第n行元素
            EnterQueue(&q, temp); //可以利用画图理解
        }
        DeleteQueue(&q, &x);
        printf("%6d", x); //打印n-1行最后一个元素
        EnterQueue(&q, 1);
        printf("\n");
    }
    while (!IsEmpty(&q)) { //打印最后一行
        DeleteQueue(&q, &x);
        printf("%6d", x);
    }
}
//主函数: 
int main() {
    int N;
    printf("请输入想打印的行数:");
    scanf("%d", &N);
    YangHuiTriangle(N);
    printf("\n");
    return 0;
}


http://www.ppmy.cn/news/893808.html

相关文章

nRF52832调试

需要用蓝牙芯片做无线串口&#xff0c;选择使用nRF52832。taobao上买了一个hc-42的小板子自己调试用。 1 第一步&#xff0c;测试板子串口转蓝牙是否正常--正常 2 第二步&#xff0c;烧写程序 将板子上的SWDIO、SWDCLK、VCC、GND都焊线出来&#xff0c;一开始使用ST-LINK&am…

22.3.13

1&#xff0c;费解的开关&#xff0c;&#xff08;性质&#xff0c;思路&#xff09;2&#xff0c;递归实现组合型枚举&#xff08;改正&#xff0c;方法&#xff09;复习其他几种的排列&#xff0c;指数型枚举及优化&#xff0c;排列型枚举及优化&#xff1b; 1&#xff0c;费…

s123

mucus plug hand out&#xff1a;分发 contraction stuffed Chunky bumper uterus Quartz ice chips: 碎冰 obstetrician chubby resent hypothetically fundamentally knapsack dressy-dress 盛装亮相 not much of&#xff1a;算不上 cramp pastry sinister loaner vent crown…

LabVIEW开发惯性测量系统

LabVIEW开发惯性测量系统 惯性导航系统是通过将惯性传感器直接绑定在载体主体上来完成制导和导航任务的系统。所以惯性测量系统主要是动态静态地测试陀螺仪和加速度计的性能。测试点和计算点数众多&#xff0c;对测试速度和精度要求高。基于上述特点&#xff0c;基于虚拟仪器软…

S32软件使用

1.在S32代码编辑区的左侧滑条上右击可快速设置显示行号等设置。 2.在Debug时&#xff0c;可先暂停后&#xff0c;在Expressions菜单栏中修改某个变量的值。

【Nordic】使用nRF52810常见问题

文章目录 问题1&#xff1a;Nordic调用蓝牙协议栈Crash问题问题2&#xff1a;如何使用MDK下载Application和SoftDevice的HEX文件问题3&#xff1a;如何使用JFlash下载Application和SoftDevice的HEX文件问题4&#xff1a;nRF52810如何选择SoftDevice版本问题5&#xff1a;如何使…

nrf52840蓝牙协议栈样例分析

蓝牙SDK的example 文件夹提供了开发BLE的模板工程&#xff0c;它具有通用性&#xff0c;可以为自己开发工程提供参考。打开examples\ble_peripheral\ble_app_template文件夹下的main.c文件&#xff0c;主函数main的内容为&#xff1a; /**brief Function for application main…

NRF52833学习笔记(1)——搭建环境、编译烧写

一、搭建环境 1.1 安装Keil 5 官网下载&#xff1a;http://www2.keil.com/mdk5/ 百度网盘&#xff1a;https://pan.baidu.com/s/1T_eF5NDYeq38bR0cqjiZkw 提取码&#xff1a;562z 1.2 下载SDK 官网下载&#xff1a;https://www.nordicsemi.com/Software-and-tools/Software…