题目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210404165459402.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqaHNx,size_16,color_FFFFFF,t_70)
答案
#include<stdio.h>
int top=0;
void pop()
{
top--;
}
void push()
{
top++;
}
int main()
{
int n,max,i,j,flag=0;
scanf("%d %d",&n,&max);
getchar();
char str[200];
for(i=0;i<n;i++)
{
gets(str);
for(j=0;str[j];j++)
{
if(str[j]=='S')
{
if(top==max) flag=1;
push();
}
else if(str[j]=='X')
{
if(top==0) flag=1;
pop();
}
}
if(flag==0&&top==0) printf("YES\n");
else printf("NO\n");
flag=0;
top=0;
}
}
问题总结
- 如果使用
gets()
函数,那就要在输入两个整数后加上getchar()
;而如果使用scanf
就不需要了 - 内外循环的变量要区分开,例如i,j,不要都用i
- 注意审题,最后判断时不仅需要没有非法操作,还要堆栈为空(即
top==0
) - 每次循环后要记得将flag以及top变量还原