Parentheses Balance
题意:括号匹配
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char ch, stack[200]; int i = 0, flag = 0, n, j; scanf("%d%*c", &n); while(n--) { memset(stack, 0, sizeof(stack)); while (scanf("%c", &ch) != EOF && ch != ‘\n‘) { if (ch == ‘(‘ || ch == ‘[‘) { i++; stack[i] = ch; } else if (ch == ‘)‘ || ch == ‘]‘) { if (stack[i] == ‘(‘ && ch == ‘)‘ || stack[i] == ‘[‘ && ch == ‘]‘) i--; else flag = 1; } else flag = 1; } printf("%s\n", (i == 0 && flag == 0) ? "Yes" : "No"); flag = 0; i = 0; } return 0; }
原文地址:http://www.cnblogs.com/Susake/p/3841014.html