当前位置: 首页 > news >正文

第3章 栈和队列 编程题

PS : 这函数题真的跟X一样,非要搞起来显得那么复杂,还得跟着它那令人心碎的逻辑走,等以后要是有心情了再考虑补上

目录

7-1 队列的实现及基本操作

7-2 队的基本操作 

7-3 h0181. 约瑟夫问题

7-4 括号匹配

7-5 判断回文

7-6 模拟栈

7-7 模拟队列

7-8 队列操作

7-9 汉诺塔(Lanqiao)

7-10 表达式转换

7-11 逆波兰表达式求值


7-1 队列的实现及基本操作

// 数组模拟
#include<stdio.h>
int main()
{
    int N,command,queue[100001],head=0,tail=0; // 模拟队列
    scanf("%d",&N);
    while (N--)
    {
        scanf("%d",&command);
        if(command==1) scanf("%d",&queue[tail++]);
        else if(head==tail) printf("invalid\n");
        else printf("%d\n",queue[head++]);
    }
    return 0;
}

7-2 队的基本操作 

http://t.csdn.cn/CiXx0


7-3 h0181. 约瑟夫问题

#include<stdio.h>
int main()
{
    int n,m,result;
    while (scanf("%d %d",&n,&m) && (n!=0 || m!=0) ){
        result = 1;
        for(int z=1;z<=n;z++) result = (result+m)%z;
        printf("%d\n",result+1);
    }
    return 0;
}
#include<stdio.h>
#include<stdlib.h>


struct Queue{
    int data;
    struct Queue* next;
}*queue,*pop;


struct Queue *createQueue(int len);   // 生成长度为len循环队列
int main()
{
    int n,m;
    while (scanf("%d %d",&n,&m) && (n!=0 || m!=0)){
        queue = createQueue(n);
        while (queue->next!=NULL)
        {
            for(int z=1;z<m;z++) queue=queue->next;
            if(queue->next->next!=queue)
            {
                pop = queue->next;
                queue->next = pop->next;
                free(pop);
            }
            else
            {
                free(queue->next);
                queue->next = NULL;
            }
        }
        printf("%d\n",queue->data-1);
        free(queue);
    }
    return 0;
}
// 生成循环队列
struct Queue *createQueue(int len)
{
    struct Queue *head,*body;
    head = (struct Queue *) malloc(sizeof(struct Queue));
    head->data = 1;     // 0 默认空数据
    body = head;
    for(int z=2;z<=len;z++){
        body->next = (struct Queue *) malloc(sizeof(struct Queue));
        body = body->next;
        body->data = z;
    }
    body->next = head;
    return head;
}

7-4 括号匹配

#include<stdio.h>

int flag(char a,char b);
int main()
{

    char str[2022],stack[502];
    int N,top;
    scanf("%d",&N);
    getchar();
    while (N--)
    {
        gets(str);
        top = 0;
        for(int z=0;str[z]!='\0';z++)
        {
            if(str[z]=='(' || str[z]=='{' || str[z]=='[') stack[++top] = str[z];
            else if(str[z]==')' || str[z]=='}' || str[z]==']')
            {
                if(flag(stack[top],str[z])) top--;
                else
                {
                    top = -1;
                    break;
                }
            }
        }
        top == 0 ? puts("Yes") : puts("No");
    }

    return 0;
}
int flag(char a,char b)
{
    if(a=='{' && b=='}') return 1;
    if(a=='(' && b==')') return 1;
    if(a=='[' && b==']') return 1;
    return 0;
}

7-5 判断回文

#include <stdio.h>
#include <string.h>
int main()
{
    char s[520];
    gets(s);
    
    int len = strlen(s),flag = 1;
    for(int z=0;z<len/2;z++) if(s[z]!=s[len-z-1]) {
        flag = 0;
        break;
    }
    
    for(int z=0;z<len;z++) putchar(s[z]);
    flag == 1 ? puts("是回文。") : puts("不是回文。");
    return 0;
}

7-6 模拟栈

 

#include<stdio.h>

int main()
{
    int stack[200408],top=0,N,command;
    char str[200814];
    scanf("%d",&N);
    while (N--)
    {
        scanf("%s",&str);
        command = 0;
        for(int z=0;str[z]!='\0';z++) command += str[z];
        switch (command) {
            case 448: scanf("%d",&stack[top++]); break;
            case 335: top--;break;
            case 559: top==0 ? puts("YES") : puts("NO"); break;
            case 566: printf("%d\n",stack[top-1]);break;
        }
    }

    return 0;
}

7-7 模拟队列

#include<stdio.h>

int main()
{
    int queue[200408],head=0,tail=0,N,command;
    char str[200814];
    scanf("%d",&N);
    while (N--)
    {
        scanf("%s",&str);
        command = 0;
        for(int z=0;str[z]!='\0';z++) command += str[z];
        switch (command) {
            case 448: scanf("%d",&queue[tail++]); break;
            case 335: head++;break;
            case 559: head==tail ? puts("YES") : puts("NO"); break;
            case 566: printf("%d\n",queue[head]);break;
        }
    }

    return 0;
}

7-8 队列操作

#include<stdio.h>

int main()
{
    int queue[200408],head=0,tail=0,N,command;
    scanf("%d",&N);

    for(int z=0;z<N;z++)
    {
        scanf("%d",&command);
        switch (command) {
            case 1: scanf("%d",&queue[tail++]); break;
            case 2:
                if(head<tail) printf("%d\n",queue[head++]);
                else puts("no"),N = 0;
                break;
            case 3: printf("%d\n",tail-head);
        }
    }
    return 0;
}

7-9 汉诺塔(Lanqiao)

#include<stdio.h>
int N,M,result=0;
void N0_ON(int deep,char a,char b,char c)
{
    if(deep==0) return;
    N0_ON(deep-1,a,c,b);
    if(++result==M) printf("#%d: %c->%c\n",deep,a,c);
    N0_ON(deep-1,b,a,c);
}
int main()
{
    scanf("%d %d",&N,&M);
    N0_ON(N,'A','B','C');
    printf("%d",result);
    return 0;
}

7-10 表达式转换

http://t.csdn.cn/2HGe5


7-11 逆波兰表达式求值

#include<stdio.h>
int atoi(char *key);    // 字符串转数字
int main()
{
    int stack[2022]={0},top=0,num;
    char str[88480];
    while (scanf("%s",&str)!=EOF)
    {
        if(str[0]=='+' || str[0]=='-' || str[0]=='*')
        {
            num = stack[--top];
            switch (str[0]) {
                case '+': stack[top-1]+=num; break;
                case '-': stack[top-1]-=num; break;
                case '*': stack[top-1]*=num; break;
            }
        }
        else stack[top++] = atoi(str);
    }
    printf("%d",stack[0]);
    return 0;
}
int atoi(char *key)
{
    int result = 0;
    for(int z=0;key[z]!='\0';z++) result = result*10+key[z]-'0';
    return result;
}

相关文章:

  • Redis面试
  • python正则表达式(三)
  • 雷达信号处理算法:静态杂波滤除(附MATLAB代码和数据)
  • Doing It in User Space
  • Vue2:网易云播放音乐并实现同步一次显示一行歌词
  • 这四个问题处理好,无人机集群编队研究会有新突破
  • 【JavaSE】之JVM入门(上)
  • 《计算机视觉基础知识蓝皮书》第7篇 模型优化方法及思路
  • java毕业设计牙科诊所管理系统Mybatis+系统+数据库+调试部署
  • 蓝桥杯2022年(本科c++b组)
  • pytorch :OSError: [WinError 1455] 页面文件太小,无法完成操作。 Error loading 【已解决】
  • 【BData12】Hadoop HDFSMapReduse
  • 阿里MySQL应用实战与性能调优手册惨遭泄漏,GitHub下载量超23K+
  • 软件测试简历包装我们会了,但测试人的自我“包装”呢?HR自我介绍包装小技巧【建议收藏】
  • Spring 注解开发下的依赖注入(自动装配)(引用类型)(普通类型)(加载properties文件)
  • [case10]使用RSQL实现端到端的动态查询
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • Docker容器管理
  • JAVA SE 6 GC调优笔记
  • Linux后台研发超实用命令总结
  • SpiderData 2019年2月13日 DApp数据排行榜
  • VUE es6技巧写法(持续更新中~~~)
  • Vue2.0 实现互斥
  • Vue全家桶实现一个Web App
  • 从PHP迁移至Golang - 基础篇
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 互联网大裁员:Java程序员失工作,焉知不能进ali?
  • 机器学习中为什么要做归一化normalization
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 区块链分支循环
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 数据科学 第 3 章 11 字符串处理
  • 算法---两个栈实现一个队列
  • 微信小程序开发问题汇总
  • 因为阿里,他们成了“杭漂”
  • 自动记录MySQL慢查询快照脚本
  • ​Linux·i2c驱动架构​
  • ​人工智能书单(数学基础篇)
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • (C语言)fgets与fputs函数详解
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (四) Graphivz 颜色选择
  • ***测试-HTTP方法
  • .Net Remoting常用部署结构
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET 事件模型教程(二)
  • .net 中viewstate的原理和使用
  • .Net8 Blazor 尝鲜
  • .py文件应该怎样打开?
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • [ NOI 2001 ] 食物链
  • [2016.7 day.5] T2