宇神完成了栈的实验要求后,他又很是开心,刚要出去五排, 菌菌子突然问道老师让做的队列的那个实验你写完了么,宇神顿时大呼悲哉。。。。他给忘记了,怎么办。。明天就要上交实验报告了,你能帮他搞定么???
你需要完成三种操作1.enqueue x,将元素x插入队尾。2.dequeue,若队列非空,则删去队头元素,并输出该元素。3.query,从队头开始删除所有元素,并输出。
Input
本题有多组测试数据,每组数据首先输入一个T,接下来T行是T种对队列的操作。 (0< T < 100,0< x <= 500)
Output
每次执行dequeue操作时删除队头元素输出并换行,如果队列为空输出“this is empty!”并换行。
每次执行query操作时删除所有元素队列内所有元素并输出,每个元素占一行,如果栈为空输出“this is empty!”并换行。
每组数据后有一个空行。
Sample Input
10 enqueue 1 enqueue 2 enqueue 3 enqueue 4 query dequeue enqueue 1 dequeue query dequeue
Sample Output
1 2 3 4 this is empty! 1 this is empty! this is empty!
代码如下:
#include <stdio.h>
#define max 10000
char s[5000];
int q[max];
int front,back;
void enqueue(int x)
{
q[back]=x;
back++;
}
void pd()
{
if(back==front)
printf("this is empty!\n");
}
int dequeue()
{
printf("%d\n",q[front]);
while(front<back)
{
q[front]=q[front+1];
front++;
}
front=0;
back-=1;
}
int query()
{
while(front<back)
{
printf("%d\n",q[front]);
q[front]=0;
front++;
}
front=back=0;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
back=0;
getchar();
int x;
while(t--)
{
scanf("%s",s);
if(s[0]=='e')
{
scanf("%d",&x);
enqueue(x);
}
else if(s[0]=='d')
{
pd();
if(front!=back)
dequeue();
}
else if(s[0]=='q')
{
pd();
if(front!=back)
{
query();
}
}
}
printf("\n");
}
return 0;
}
A - A
现在有n个元素分别是1,2,3,...,n,我们想知道通过一个栈,在n次push/pop后,出栈序列可能是什么样的。例如n是5,那么入栈次序就是1,2,3,4,5,如果我们希望出栈次序同样是1,2,3,4,5,那只要每push一个数,就立即pop一个数。如果我们希望出栈次序是3,2,4,5,1,那么我们先让1,2,3入栈,然后pop出来3和2,接着4入栈后马上pop,再就是5入栈后马上pop,最后再把栈里的1pop出。再例如,如果我们希望出栈次序是5,4,1,2,3,这是办不到的,如果要让5最先出栈,那么出栈次序只可能是5,4,3,2,1
Input
输入由多块输入构成,每块的第一行是n,(1≤n≤1000),接着是多组测试数据,每组一行,每行由n个1到n的整数组成,直到某一行第一个数是0表示此输入块结束,然后是下一块数据。
当某一块的第一行的n是0的时候结束所有输入
Output
对每组数据,只要输出Yes或No,Yes表示能产生那样的出栈序列,No表示不能。 输入块与输入块之间要多输出一个空行分隔。注意对于最后一个n为0的输入块不要多输出空行。
Sample Input
5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0
代码如下:
#include <stdio.h>
int a[10000];
int i,j,h;
int main()
{
int n;
while(scanf("%d",&n)&&n!=0)
{
while(1)
{
int flag=0;
int dsy=0;
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(a[0]==0)
{
dsy=1;
printf("\n");
break;
}
}
if(dsy==1)break;
for(h=0; h<n; h++)
{
dsy=a[h];
for(j=h+1; j<n; j++)
{
if(a[h]>a[j])
{
if(dsy<a[j])
flag=1;
else
dsy=a[j];
}
}
}
if(flag==1)
printf("No\n");
if(flag==0)
printf("Yes\n");
}
}
return 0;
}
下面还有一个理解错题意的代码
F - F
冰冰子最近新学习了队列和栈两种重要的数据结构,他知道它们具有push 和pop操作。
而冰冰子现在想同时研究栈和队列,于是,他开始了一个实验。
现在,假设队列和栈都是空的。给定一系列push k和pop操作之后,冰冰子想知道队列和栈中分别存的数字。若队列或栈已经空了,仍然接收到pop操作,则输出error。
Input
第一行为m,表示有m组测试输入,m<100。
每组第一行为n,表示下列有n行push k或pop操作。(n<150)
接下来n行,每行是push k或者pop,其中k是一个整数。
(输入保证同时在队列或栈中的数不会超过100个)
Output
对每组测试数据输出两行,第一行是队列情况,若在队列空时收到pop操作,则输出error。其余情况将队列按照对头至队尾顺序输出队列中所有元素,中间用空格隔开。第二行是栈的情况,若在栈空时收到pop操作,则输出error。其余情况下按照栈底至栈顶的顺序输出栈中所有元素。
Sample Input
2 4 push 1 push 3 pop push 5 1 pop
Sample Output
3 5 1 5 error error
#include <stdio.h>
#include <string.h>
int front1,front2,top1,top2;
int q[1000],p[1000];
char c[20];
int push(int x)
{
q[top1]=x;
top1++;
p[top2]=x;
top2++;
}
void pop1()
{
while(front1<top1)
{
q[front1]=q[front1+1];
front1++;
}
front1=0;
top1-=1;
}
void pop2()
{
front2=0;
top2-=1;
}
void pd()
{
if(top1==front1)
printf("error\n");
if(top2==front2)
printf("error");
}
int main()
{
int m;
scanf("%d",&m);
while(m--)
{
int n,x;
scanf("%d",&n);
while(n--)
{
scanf("%s",c);
if(strcmp(c,"push")==0)
{
scanf("%d",&x);
push(x);
}
if(strcmp(c,"pop")==0)
{
pd();
{
if(top1!=front1)
pop1();
if(top2!=front2)
pop2();
}
}
}
if(top1!=front1)
while(front1<top1)
{
printf("%d ",q[front1]);
front1++;
}
printf("\n");
if(top2!=front2)
while(top2-1>=front2)
{
printf("%d ",p[top2-1]);
top2--;
}
printf("\n");
}
return 0;
}