UVa 10077 - The Stern-Brocot Number System

题目:给你一颗分数组成的二叉树,初始值是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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值