Uva 712 S-Trees

点击打开链接http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=653


解题思路:我们知道这颗二叉树比较特殊,是一颗满二叉树,而且每一层上面的节点值都是相同的,那么我们可以分析一下数据。

数据:
3
x1 x2 x3
00000111
4
000
010
111
110
我们知道对于000这组数据肯定是最左的节点,那么111是最右的,所以只要求出后面操作的和,做差就是第几个叶子节点的值
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

const int MAXN = 1000;
int n , m , sum;
string str[10] , mstr[MAXN];//str用来存储操作的步骤
char num[MAXN];//存储叶子节点的值

//输入函数
void input(){
    for(int i = 0 ;i < n ; i++)
        cin>>str[i];
    cin>>num;
}
//处理函
void solve(int k){
    cin>>m;
    int temp;
    for(int i = 0 ; i < m ; i++)
        cin>>mstr[i];
    printf("S-Tree #%d:\n" , k);
    for(int i = 0 ; i < m ; i++){
         temp = 0;
         for(int j = 0 ; j < n ; j++)
             temp += (mstr[i][j] - 48) * pow(2 , n - j -1);
         printf("%c" , num[temp]);
    }
    cout<<endl<<endl;
}
//主函数
int main(){
    int i = 1;
    while(scanf("%d" , &n) && n){
        sum = pow(2 , n);
        input();
        solve(i);
        i++;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值