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