首页 > 代码库 > 之前就写好了,but……数组开小了……!!!
之前就写好了,but……数组开小了……!!!
#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……数组开小了……!!!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。