S-Trees UVA - 712

https://vjudge.net/problem/UVA-712

没有必要真的要把这颗树给它建起来,可以用一个数组保存每一层上节点是0还是1,然后从根节点开始模拟它的走向,直到走到叶节点

程序中使用到了一个将字符串转化为数字的函数

关于这个函数可以在这篇博客中了解https://blog.csdn.net/qq_41776911/article/details/80194359

vs2017中的scanf_s函数在vjudge中提交会提示错误,好坑

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
const int N = 128 + 10;
int leaf[N];
int depth[10], invans[10];

int main()
{
	int n, m;
	for (int kcase = 1; cin >> n; kcase++)
	{
		if (n == 0) break;
		getchar();
		char s[N];
		for (int i = 0; i < n; i++) {
			scanf("%s", s);
			invans[atoi(s + 1) - 1] = i;
			//cout << ans[i] << endl;
		}
		scanf("%s", s);
		for (int i = 0; i < strlen(s); i++) leaf[i] = s[i] - '0';
		//cout << leaf[0] << endl;
		cin >> m; vector<int> ans;
		while (m--) {
			scanf("%s", s);
			for (int i = 0; i < n; i++) 
				depth[invans[i]] = s[i] - '0';
			int i = 0, d = 0;
			while (d < n) {
				if (depth[d]) i = 2 * i + 2;
				else i = 2 * i + 1;
				d++;
			}
			ans.push_back(leaf[i - (1 << n) + 1]);
		}
		printf("S-Tree #%d:\n", kcase);
		for (auto i : ans) cout << i;
		cout << endl << endl;
		
	}
	//system("pause");
}
/*
3 
x1 x2 x3 
00000111 
4 
000 
010 
111 
110 
3 
x3 x1 x2 
00010011 
4 
000 
010 
111 
110 
0 
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值