1:用一根数轴,根结点是坐标index,左结点-1,右结点+1
还有一个0ms的不知道怎么过的
#include#include #include #include #include #include using namespace std;void caculate(int* tree, int index, int* min, int*max){ int s; cin >> s; if (s != -1) { tree[index - 1] += s; if ((index - 1) < *min) { *min = index-1; } caculate(tree, index - 1, min, max); } cin >> s; if (s != -1) { tree[index + 1] += s; if ((index +1) > *max) { *max = index+1; } caculate(tree, index + 1, min, max); }}int main(){ freopen("d:\\1.txt", "r", stdin); int MAXN = 10000; int number = 1; while (cin) { int MIDDLE = 5000; int total[MAXN]; memset(total, 0, sizeof(int) * MAXN); int s; cin >> s; if (s == -1) return 0; else { int max = MIDDLE; int min = MIDDLE; total[MIDDLE] += s; caculate(total, MIDDLE, &min, &max); cout << "Case " << number << ":" << endl; for (int i = min; i <= max; i++) if (i == min) cout << total[i]; else cout << " " << total[i]; cout << endl<
posted on 2017-05-10 16:34 阅读( ...) 评论( ...)