您的位置:首页 > 其它

UVa 10077 - The Stern-Brocot Number System

2015-03-25 17:17 501 查看
题目:给你一颗分数组成的二叉树,初始值是1/1,两边的边界分别是0/1与1/0,然后递归建立子树节点,

每个子树的节点值为两边的边界值得分子之和比上分母之和,新的值也加入边界值。

分析:递归,数据结构。看到上面的描述就可以做了吧,直接递归。

tree(L, R, key) {

if(add(L+R)= key)return;

if(add(L+R)< key){

cout << "R";

tree(L,add(L,R),key);

}else {

cout << "L";

tree(add(L,R),R,key);

}

}

说明:强大的递归╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

void tree(int Lx, int Ly, int Rx, int Ry, int Tx, int Ty) 
{
	if (Lx+Rx == Tx && Ly+Ry == Ty) {
		printf("\n");
		return;
	}
	if ((Lx+Rx)*Ty < (Ly+Ry)*Tx) {
		printf("R");
		tree(Lx+Rx, Ly+Ry, Rx, Ry, Tx, Ty);
	}else {
		printf("L");
		tree(Lx, Ly, Lx+Rx, Ly+Ry, Tx, Ty);
	}
}

int main()
{
	int n,m;
	while (cin >> n >> m && !(m == 1 && n == 1))
		tree(0, 1, 1, 0, n, m);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: