题目:
题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=653
题意:
给出满二叉树叶结点的所有值,输入0代表向左走输入1代表向右走,输出最后到达的叶子的值。
思路:
根据满二叉树的性质,向左走编号*2,向右走编号 * 2 + 1;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,tt=1;
while(~scanf("%d",&n) && n)
{
char s[3],s1[5000],s2[5000],str[5000];
int num[5000],a[5000],i;
for(i=1;i<=n;i++)
scanf("%s",s);
scanf("%s",str);
int len=strlen(str);
for(i=len;i<2*len;i++)
num[i]=str[i-len]-'0';
int k=0;
int m,t;
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s",&s1);
t=1;
for(int j=0;j<n;j++)
{
if(s1[j]=='0')
t=t*2;
else t=t*2+1;
}
a[k++]=num[t];
}
printf("S-Tree #%d:\n",tt++);
for(i=0;i<k;i++)
printf("%d",a[i]);
printf("\n\n");
}
return 0;
}