习题6-2 UVA 712 S-Trees S树

看了紫书翻译后,感觉很简单,但不解有些条件!

尝试做做,果然是一道水题,条件很多没用的!

首先给你的X1,X2,X3是没用的,

直接根据深度,求出最后一层的编号!  第一个2 ^ n;而且  n <=7随便怎么做就可以了!

直接对查询进行处理0向左拐,1右拐!

最后放到字符串里输出即可!

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int depth;
int A[maxn],cnt2;
int main()
{
    while(scanf("%d",&depth) == 1 && depth){
        char str[maxn];
        for (int i = 0; i < depth; ++i)scanf("%s",str);
        scanf("%s",str);
        int len = strlen(str);
        for (int i = 0; i < len; ++i){
            A[(1 << depth) + i] = str[i] - '0';
        }
        int n;
        char ans[maxn],cnt = 0;
        scanf("%d",&n);
        for (int i = 0; i < n; ++i){
            scanf("%s",str);
            len = strlen(str);
            int sum = 1;
            for (int i = 0; i < len; ++i){
                if (str[i] == '0')sum *= 2;
                else sum = sum * 2 + 1;
            }
            ans[cnt++]=A[sum]+48;
        }
        ans[cnt]= 0;
        printf("S-Tree #%d:\n%s\n\n",++cnt2,ans);
    }
    return 0;
}



你可能感兴趣的:(C语言,uva)