UVA 112 – Tree Summing(棧)

來源:互聯網
上載者:User

        又是一道讓我心碎的題。UVA上提交了10幾次都錯了,在北大上又過了。用棧做的,在處理輸入和判斷括弧上感覺還蠻良好的。

#include <stdio.h>#include <string.h>#define MAXN 10000int stack[MAXN];int topc, top, t;bool judge() {    int sum = 0;    for (int i=1; i<=top; i++)        sum += stack[i];    if (sum == t)        return true;    return false;}int main() {    //freopen("f:\\out.txt", "w", stdout);    while (scanf("%d", &t) != EOF) {        int tmp = 0, flag = 0, isNeg = 0;        char pre[4];        topc = top = 0;        memset(pre, 0, sizeof (pre));        while (1) {            // 接收字元的代碼,忽略掉空格和換行            char ch = getchar();            while ('\n'==ch || ' '==ch)                ch = getchar();            // 記錄該字元前三個字元,便於判斷是否為葉子            pre[3] = pre[2];            pre[2] = pre[1];            pre[1] = pre[0];            pre[0] = ch;            // 如果遇到左括弧就進棧            if ('(' == ch) {                topc++;                if (tmp) {                    if (isNeg) {                        tmp *= -1;                        isNeg = 0;                    }                    stack[++top] = tmp;                    tmp = 0;                }                continue;            }            // 如果遇到右括弧就出棧            if (')' == ch) {                // 如果為葉子便計算                if ('('==pre[1] && ')'==pre[2] && '('==pre[3]) {                    if (!flag)                        flag = judge();                }                else if (pre[1] != '('){                    top--;                }                topc--;                // 如果左括弧都被匹配完說明二叉樹輸入完畢                if (!topc)                    break;                continue;            }            if ('-' == ch)                isNeg = 1;            else                tmp = tmp*10 + (ch-'0');        }        if (flag)            printf("yes\n");        else            printf("no\n");    }    return 0;}

下面是一個牛人的代碼,膜拜。忘了在哪找的了。

#include<iostream>using namespace std;bool ok;bool tree_sum(int n,int sum){    int v;    char ch;    cin>>ch;    if(!((cin>>v)==0))    {        n+=v;        bool t=tree_sum(n,sum)|tree_sum(n,sum);        if(!ok&&!t)            ok=(n==sum);        cin>>ch;        return true;    }    else    {        cin.clear();//消除錯誤狀態        cin>>ch;        return false;    }}int main(){    // freopen("f:\\out.txt", "w", stdout);    int sum;    while(cin>>sum)    {        ok=false;        tree_sum(0,sum);        cout<<(ok?"yes":"no")<<endl;    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.