洛谷 P1739 表達式括號匹配

水題

幾個注意點:
1. 可以用cnt來代替棧
2. 對於左括號與右括號 要分別處理

//P1739 表達式括號匹配
//2016.11.23 

#include <cstdio>
#include <iostream>
using namespace std;

char c;
int cnt = 0;   //用來代替棧 

int main(){
    while (scanf("%c", &c) == 1){
        if (c == '@') break; 
        if (c == '(') cnt++;
        if (c == ')')   //注意! 要防止像這樣的情況:))(( 
            if (cnt > 0) cnt--;
            else {
                printf("NO");
                return 0;
            }
    }

    if (!cnt) printf("YES");
    else printf("NO");

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章