题目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\n"); } else if(strcmp("Pop",str)==0) { if(pop()==0) printf("failed\n"); } else if(strcmp("Query",str)==0) { scanf("%d",&k); int rt; if(query(k,rt)==0) printf("failed\n"); else printf("%d\n",rt); } else if(strcmp("Isempty",str)==0) { if(empty()==1) printf("yes\n"); else printf("no\n"); } else if(strcmp("Isfull",str)==0) { if(full()==1) printf("yes\n"); else printf("no\n"); } } } return 0; }