S-Trees UVA712(二叉树)

来源:互联网 发布:ps cs3 mac版 编辑:程序博客网 时间:2024/06/10 06:15

题目读了半天,差点被吓住!题目本身很简单,就是一颗满二叉树,向左(2*temp),向右(2*temp+1),最后减去(1<<n)-1;(即非叶子结点的个数,因为储存叶子结点是从下表为1开始的)。

#include<cstdio>#include<cstring>using namespace std;const int maxn=1<<8;const int maxn2=10000;char X[8][2];char Term[maxn];char Common[maxn2];int main(){int n;int count=1;while(scanf("%d",&n)==1 && n){memset(Common,0,sizeof(Common)); //刚开始忘记清零,WA了一次for(int i=0;i<n;i++)scanf("%s",X[i]);           //变量的输入本题中没有作用scanf("%s",Term+1);printf("S-Tree #%d:\n",count++);char s[8];int m;scanf("%d",&m);int k=0;for(int i=0;i<m;i++){scanf("%s",s);int temp=1;for(int j=0;j<strlen(s);j++){if(s[j]=='0')temp=2*temp;else temp=2*temp+1; }Common[k++]=Term[temp-(1<<n)+1];}printf("%s\n\n",Common);}    return 0;}


0 0
原创粉丝点击