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);
}
}
说明:强大的递归╮(╯▽╰)╭。
每个子树的节点值为两边的边界值得分子之和比上分母之和,新的值也加入边界值。
分析:递归,数据结构。看到上面的描述就可以做了吧,直接递归。
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; }
相关文章推荐
- UVa 10077 The Stern-Brocot Number System (数学)
- uva 10077 - The Stern-Brocot Number System
- uva 10077 The Stern-Brocot Number System
- UVa Problem 10077 The Stern-Brocot Number System (Stern-Brocot 代数系统)
- UVa Problem Solution: 10077 - The Stern-Brocot Number System
- 10077 - The Stern-Brocot Number System
- The Stern-Brocot Number System(排序二进制)
- 110507 The Stern-Brocot Number System
- (挑战编程_5_7)The Stern-Brocot Number System
- UVA 10077 The Stern-Brocot Number System
- The Stern-Brocot Number System(排序二叉树)
- UVa 11651 Krypton Number System DP + 矩阵快速幂
- UVA11651 Krypton Number System【矩阵快速幂】
- UVA 11651 Krypton Number System(矩阵加速DP)
- UVA 11651 - Krypton Number System(DP+矩阵快速幂)
- uva 11651 - Krypton Number System(矩阵快速幂)
- UVA 11078 Open Credit System(扫描 维护最大值)
- Find n‘th number in a number system with only 3 and 4
- UVA 4683 - Find The Number
- uva 11078 Open Credit System