码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 673 括号平衡

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

标签:uva      括号   匹配   

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

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 括号平衡

标签:uva      括号   匹配   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!