zoukankan      html  css  js  c++  java
  • 括号配对问题(南阳2)

    //括号匹配的检验,运用栈的思想,构造栈不要想的太复杂。在此题中事实上也就是一个数组。 
    /*书上所说的可用“期待的急迫程度”,比如考虑下列括号序列:
        【(【】【】)】问括号是否匹配
        */ 
    #include<cstdio>
    #include<cstring>
    int main()
    {
        int top,i;
        char a[1010],b[1010];
        while(scanf("%s",a)!=EOF)
        {
            top=1;                         //top所指向b数组下标的下一位
            b[top++]=a[0];           
            for(i=1; i<strlen(a); i++)
            {
                if(a[i]=='('||a[i]=='[')  //此种情况,进栈 
                    b[top++]=a[i];
                else if(a[i]==')'&&b[top-1]=='(')  //出栈,事实上也就是对b数组的覆盖
                    top--;        
                else if(a[i]==']'&&b[top-1]=='[') 
                    top--; 
                else
                {                             //剩下的两种情况都要进栈 
                    b[top++]=a[i];                
                }
            }
            if(top==1)
                printf("括号匹配! ");
            else
                printf("括号不匹配!!! ");
        }
        return 0;

     
  • 相关阅读:
    进程,线程(thread)
    PHP权限分配思路
    xml simpleXML_load_file(), simpleXML_load_string()
    XML基础
    php QQ登录
    ci验证码
    jquery中的 .html(),.val().text()
    ci 用本身 email 类发 email
    Objective-C 装饰模式--简单介绍和使用
    Objective-C 外观模式--简单介绍和使用
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5111888.html
Copyright © 2011-2022 走看看