Ref: ITSA 58 - [Problem 3] 完整二元樹
/*******************************************************/
/* [Problem 3] 完整二元樹 */
/* Author: awei0905 [at] awei0905.blogspot.tw */
/* Version: 2017/12/04 */
/*******************************************************/
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int m;
cin >> m;
while(m--) {
int C, index = 1, binTree[16];
char buf[100];
cin >> C;
cin.ignore(0x7fffffff, '\n');
cin.getline(buf, 101, '\n');
for(int i = 0; buf[i] != '\0'; i++)
if(buf[i] >= '0' && buf[i] <= '9') {
binTree[index++] = atoi(&buf[i]);
while(buf[i+1] >= '0' && buf[i+1] <= '9') i++;
}
int innerNodes = floor(index/2);
bool notFirstOut = false;
for(int i = 1; i < innerNodes; i++) {
if(abs(binTree[i] - binTree[i * 2]) <= C) {
if(notFirstOut) cout << " ";
cout << char('@' + i) << char('@' + i * 2);
notFirstOut = true;
}
if(abs(binTree[i] - binTree[i * 2 + 1]) <= C) {
if(notFirstOut) cout << " ";
cout << char('@' + i) << char('A' + i * 2);
notFirstOut = true;
}
}
cout << endl;
}
return 0;
}
Debug: I/O需修補的路段進行標示,然後統計出答案。
2
8
(A,2),(B,10),(C,13)
7
(A,2),(B,10),(C,13),(D,7),(E,8),(F,9),(G,11)
AB
BD BE CF CG