您的位置:首页 > 大数据 > 人工智能

HDOJ1702 ACboy needs your help again!

2011-03-16 21:20 495 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1702

定义字符串数组的时候原来是这样写的:char s[4],p[3];结果总是不对,调了半天啊。。。

看来开数组的时候只要不影响空间可以适当开大一点。。。。。

AC代码:

#include<stdio.h>
#include<string.h>
#define maxsize 1000
int main(){
int n,m,temp,stack[maxsize],top,front,rear;
//用数组模拟队列、栈;
char s[5],p[4];//记录进出栈方式;
scanf("%d",&n);//number of test cases;
while(n--){
scanf("%d %s",&m,s);
//printf("***%d %s***/n",m,s);
top=0;//指向栈顶下一个元素;
front=0;//指向队头;
rear=0;//指向队尾下一个元素;
while(m--){
getchar();//接受回车符;
scanf("%s",p);
//printf("***%s/n",s);
if(strcmp(s,"FIFO")==0){//队列形式;
//printf("***/n");
if(strcmp(p,"IN")==0){//入队;
scanf("%d",&temp);//读入入栈元素;
//printf("temp=%d/n",temp);
stack[rear++]=temp;
}
else{//出队;
if(rear-front<=0){//队已空;
printf("None/n");
}
else{//队非空;
printf("%d/n",stack[front++]);
}
}
}
else{//栈形式;
if(strcmp(p,"IN")==0){//入栈;
scanf("%d",&temp);
stack[top++]=temp;
}
else{//出栈;
if(top<=0){//栈已空;
printf("None/n");
}
else{//栈非空;
printf("%d/n",stack[--top]);
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: