https://vjudge.net/problem/UVA-712
没有必要真的要把这颗树给它建起来,可以用一个数组保存每一层上节点是0还是1,然后从根节点开始模拟它的走向,直到走到叶节点
程序中使用到了一个将字符串转化为数字的函数
关于这个函数可以在这篇博客中了解https://blog.csdn.net/qq_41776911/article/details/80194359
vs2017中的scanf_s函数在vjudge中提交会提示错误,好坑
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
const int N = 128 + 10;
int leaf[N];
int depth[10], invans[10];
int main()
{
int n, m;
for (int kcase = 1; cin >> n; kcase++)
{
if (n == 0) break;
getchar();
char s[N];
for (int i = 0; i < n; i++) {
scanf("%s", s);
invans[atoi(s + 1) - 1] = i;
//cout << ans[i] << endl;
}
scanf("%s", s);
for (int i = 0; i < strlen(s); i++) leaf[i] = s[i] - '0';
//cout << leaf[0] << endl;
cin >> m; vector<int> ans;
while (m--) {
scanf("%s", s);
for (int i = 0; i < n; i++)
depth[invans[i]] = s[i] - '0';
int i = 0, d = 0;
while (d < n) {
if (depth[d]) i = 2 * i + 2;
else i = 2 * i + 1;
d++;
}
ans.push_back(leaf[i - (1 << n) + 1]);
}
printf("S-Tree #%d:\n", kcase);
for (auto i : ans) cout << i;
cout << endl << endl;
}
//system("pause");
}
/*
3
x1 x2 x3
00000111
4
000
010
111
110
3
x3 x1 x2
00010011
4
000
010
111
110
0
*/