poj1145 括号间的读取与匹配

来源:互联网 发布:java获取浏览器地址 编辑:程序博客网 时间:2024/06/06 16:42
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;char ans[110];int flag;int x;void sovle(int s, int e, int res, int x) {    if(e -1 == s)        return;   char str[30];   int k = 0, i,j;   int n = 0;   for(i = s+1; ; i++){        if(ans[i] == '(')            break;        str[k++] = ans[i];   }     str[k] = '\0';     int tem = atoi(str);  // printf("\ntem = %d\n", tem);    res += tem;  //  printf("tem = %d  res = %d \n",tem, res);   if(i + 4 == e && res == x){     flag = 1;   }    for(j = i; ; j++){     if(ans[j] == '(') n++;     if(ans[j] == ')') n--;     if(n == 0)        break;   }    sovle(i, j, res, x);    sovle(j + 1, e -1, res, x);}int main(){    char ch;    int n = 0;    int k = 0;    while(cin>>x)    {        k = 0;        n = 0;        flag = 0;        while(cin>>ch)        {            if(ch == ' ')                continue;            ans[k++] = ch;            if(ch == '(') n++;            if(ch == ')') n--;            if(n == 0)                break;        }        sovle(0, k -1, 0, x);        if(flag)            printf("yes\n");        else            printf("no\n");    }    return 0;}

0 0
原创粉丝点击