uva_112 蜜汁runtime error

   十分令我心碎的一道题,RE了10次,在编译器里过了无数样例,就是在OJ里RE。

    不说这些伤心事了,直接贴代码吧。

#include <cstdio>
#include <cstring>#include<iostream>using namespace std;#define MAXN 100000int 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() {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 (ch == '\n' || ' ' == 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;elsetmp = tmp * 10 + (ch - '0');}if (flag)printf("yes\n");elseprintf("no\n");}return 0;}


上面是RE代码。


下面这个是一位大婶的。

#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;
}

再下面这个,是真正AC的代码。


#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

char input(void)
{
	char temp;
	scanf("%c", &temp);
	while (temp == ' ' || temp == '\n')
		scanf("%c", &temp);
	return temp;
}

int deal(int v, int *leaf)
{
	int temp, value;
	scanf("%d", &value);
	temp = input();
	int max = 0, l = 0, r = 0;
	if (temp == '(') {
		if (deal(v - value, &l)) max = 1;
		temp = input();
		if (deal(v - value, &r)) max = 1;
		temp = input();
		if (l && r) max = (v == value);
	}
	else *leaf = 1;
	return max;
}

int main()
{
	int n, temp;
	while (~scanf("%d", &n)) {
		input();
		if (deal(n, &temp))
			printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

祝大家AC愉快

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值