码迷,mamicode.com
首页 > 编程语言 > 详细

之前就写好了,but……数组开小了……!!!

时间:2015-01-13 19:18:30      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>

using namespace std;
const int INF = 0xffffff;
const double Pi = 4 * atan(1);

struct Node{
    int val;
    Node * right;
    Node * left;
};
int cnt;
int a[10000];
int arr[50000];
int n,ll,rr;
void b_build(Node * &tmp){
    if(n >= cnt)
        return;
    if(a[n] == -1)
        return;
    tmp = new Node;
    tmp->left = NULL;
    tmp->right = NULL;
    tmp->val = a[n];
    if(n+1 < cnt){
        ++n;
        b_build(tmp->left);
    }
    if(n+1 < cnt){
        ++n;
        b_build(tmp->right);
    }
}

void dfs(Node * node,int num){
    if(node == NULL)
        return;
    ll = min(ll,num);
    rr = max(rr,num);
    arr[num] += node->val;
    dfs(node->left,num-1);
    dfs(node->right,num+1);
    return;
}

int main()
{
   // freopen("inpt.txt","r",stdin);
    int cas = 0;
    while(1){
        int cntN = 0;
        int cntT = 0;
        int tmp;
        cnt = 0;
        cin >> tmp;
        if(tmp == -1)
            break;
        a[cnt++] = tmp;
        cntT++;
        while(cin >> tmp){
            if(tmp == -1){
                cntN++;
            }
            else{
                cntT++;
            }
            a[cnt++] = tmp;
            if(cntN == cntT + 1)
                break;
        }
        Node * root = NULL;
        n = 0;
        b_build(root);
        memset(arr,0,sizeof(arr));
        ll = 25000;
        rr = 25000;
        dfs(root,25000);
        cout << "Case " << ++cas << ":" << endl;
        for(int i = ll;i < rr;i++)
            cout << arr[i] <<  ;
        cout << arr[rr] << endl;
        cout << endl;
    }
    return 0;
}

uva699   前序遍历建树然后dfs深搜用数组标记偏移……2333333

很简单的水题,数组开小了,一直wa……!

之前就写好了,but……数组开小了……!!!

标签:

原文地址:http://www.cnblogs.com/hanbinggan/p/4221852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!