uva-699

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int maxn=300;
int sum[maxn];
void build(int p){
    int v;cin>>v;
    if(v==-1)return;
    sum[p]+=v;
    build(p-1);build(p+1); //這一步模擬了前序遍歷
}
bool solve(){
    int v;cin>>v;
    if(v==-1) return false;
    memset(sum,0,sizeof(sum));
    int pos=maxn/2;
    sum[pos]=v;
    build(pos-1);build(pos+1);
    return true;
}
int main(){
    string line;
    while(solve()){
        int p=0;
        while(sum[p]==0)p++;
        while(sum[p]!=0)cout<<" "<<sum[p++];
        cout<<"\n";
    }
    return 0;
}
發佈了51 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章