zoukankan      html  css  js  c++  java
  • 题目1415:不一样的循环队列------注意细节小地方循环队列注意%M;还有为什么M要加一!!!!!!!!!!!!!

    #include<stdio.h>
    #include<stdlib.h>
    #include<cstring>
    
    int  queue[100001];
    int N,M;
    int front,tail;
    void init()
    {
        front=0;
        tail=0;
    }
    int empty()
    {
        if (front==tail) return 1;
        else return 0;
    }
    int full()
    {
        if(front==(tail+1)%M) return 1;
        else return 0;
    }
    int push(int k)
    {
        if(!full())
        {
            queue[tail]=k;
            tail=(tail+1)%M;
            return 1; 
        }
        return 0;
    }
    int pop()
    {
        if(!empty())
        {
            front=(front+1)%M;
            return 1;
        }
         return 0;
    }
    int query(int k,int &r)
    {
        if(k<1 || k>(tail-front+M)%M)
           return 0;
        else 
         {
             r=queue[(front+k-1)%M];
             return 1;
         } 
    }
    int main()
    { 
        int i,j,k;
        while(scanf("%d %d",&N,&M)!=EOF)
        {
            M++;////??????????为什么为什么加一 
            init();
            char str[10];
            for(i=0;i<N;i++) 
            {
                scanf("%s",str);
                if (strcmp("Push",str)==0)
                {
                   scanf("%d",&k);
                   if(push(k)==0) printf("failed
    ");    
                }
                else if(strcmp("Pop",str)==0)
                {
                    if(pop()==0) printf("failed
    ");
                }
                else if(strcmp("Query",str)==0)
                {
                    scanf("%d",&k);
                    int rt;
                    if(query(k,rt)==0) printf("failed
    ");
                    else printf("%d
    ",rt);
                        
                }
                else if(strcmp("Isempty",str)==0)
                {
                    if(empty()==1) printf("yes
    ");
                    else printf("no
    ");
                }
                else if(strcmp("Isfull",str)==0)
                {
                    if(full()==1) printf("yes
    ");
                    else printf("no
    ");
                }
                   
            }
        }    
        return 0; 
    }
  • 相关阅读:
    RESTful API设计指南
    Ubuntu16.04 安装openssl
    shell while循环
    linux awk
    vim与shell切换
    shell for循环
    css 固定宽度,自动换行
    php-fpm 与 cgi
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'
    linux ps 命令参数详解
  • 原文地址:https://www.cnblogs.com/jianrenguo/p/6550144.html
Copyright © 2011-2022 走看看