首页 > 其他 > 详细

UVa 673 括号平衡

时间:2014-05-11 20:19:04      阅读:447      评论:0      收藏:0      [点我收藏+]

思路:简单的匹配操作,利用栈。

Code:

#include<stdio.h>
#include<string.h>

char stack[135];

int main()
{
 int n;
 scanf("%d",&n);
 getchar();
 while(n-->0)
 {
  memset(stack,0,sizeof(stack));
  char c;
  int top=0;
  int flag=1;
  while((c=getchar())!=‘\n‘)
  {
   if(c==‘(‘||c==‘[‘) stack[top++]=c;
   else if(c==‘)‘)
   {
    if(top<=0||stack[--top]!=‘(‘) flag=0;    
   }                         
   else
   {//printf("top:%d s[top]:%c\n",top,stack[top-1]);
    if(top<=0||stack[--top]!=‘[‘) flag=0;//手误把字符[写成]了   
   }
  }//whilec   
  if(flag==0||top!=0) printf("No\n");
  else printf("Yes\n");        
 }//whilen
 return 0;   
}

赶紧休息,按时休息也是一种能力。。中午,晚上。

UVa 673 括号平衡,布布扣,bubuko.com

UVa 673 括号平衡

原文:http://blog.csdn.net/buxizhizhou530/article/details/25515587

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!