UVA 712-S-Trees
题目大意:给出一定深度的慢二叉树,每个叶子节点对应值,给出路径,输出得到的值,0是左子树,1是右子树
解题思路:用pow(2,n)来做很容易得到路径最后到达的位置
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int main() {
int n;
int g = 0;
while(cin >> n && n != 0) {
g++;
getchar();
int a[10];
for(int i = n-1; i >= 0; i--) {
int k;
getchar();
cin >> k;
a[k] = i;
getchar();
}
char s[1000];
gets(s);
int m;
int b[1000];
cin >> m;
getchar();
for(int i = 0; i < m; i++) {
int s = 0;
char s2[1000];
gets(s2);
for(int j = 1; j <= n; j++) {
s = s + (s2[j-1] - '0') * pow(2, a[j]);
}
b[i] = s;
}
printf("S-Tree #%d:\n", g);
for(int i = 0; i < m; i++) {
printf("%c", s[b[i]]);
}
printf("\n\n");
}
return 0;
}