UVA 699 The Falling Leaves

#include <cstdio>
int arr[82] = {0}, count = 0;

int deal(int move) {
	int left, right;
	scanf("%d", &left);
	if (left != -1) { //判断有没左子结点
		arr[move - 1] += left; //有的话	数组左移一位,并存入左子结点
		deal(move - 1);  // 判断左子结点是否有子结点
	}
	scanf("%d", &right);
	if (right != -1) {
		arr[move + 1] += right;
		deal(move + 1);
	}
	return 0;
}

int main() {
	while (scanf("%d", &arr[40]), arr[40] + 1) { //数组的arr[40] 存根结点
		printf("Case %d:\n", ++count);
		deal(40); //判断根结点有无子结点
		for (int i = 0; i < 81; i++)
			if (arr[i] != 0) //输出不为零的数组元素,并初始化该数元素
				arr[i + 1] == 0 ? printf("%d", arr[i]) : printf("%d ", arr[i]), arr[i] = 0;
		printf("\n\n");
	}
	return 0;
}


你可能感兴趣的:(UVA 699 The Falling Leaves)