UVa 712 - S-Trees

题意

给你一棵完全二叉树,输入0往左走,1往右

思路

由于是二叉树,正好相应二进制数
直接存到字符串里模拟

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    int T, t, len, i, j, num;
    string line;
    char s[1000], f[100];
    int casenum = 0;
    while( scanf("%d",&T) == 1 && T ){
        memset(s,0,sizeof(s));
        memset(f,0,sizeof(f));
        line = "";
        printf("S-Tree #%d:\n",++casenum);
        getchar();
        getline(cin,line);
        scanf("%s",s);
        scanf("%d",&t);
        while(t--){
            scanf("%s",f);
            len = strlen(f);
            num = 0;
            for( i = len-1, j = 0; i >= 0; i--, j++ )
                num += ( f[i] - '0' ) * (1<<j);
            printf("%c",s[num]);
        }
        puts("\n");
    }
    return 0;
}
posted @ 2018-02-06 20:43  JinxiSui  阅读(100)  评论(0编辑  收藏  举报