想法:
  初始化三個數L=0/1, M=1/1, R=1/0,設輸入的分數為a:

  • 如果a<M,那麼要往左邊走,
        R = M;
        M = (L分子+M分子)/(L分母+M分母);
  • 如果a>M,往右邊走,
        L = M;
        M = (R分子+M分子)/(R分母+M分母);
  • 如果a==M,停止。
這題和二分搜尋很類似。迭代算法如下:
 #include <cstdio>
using namespace std;
struct fraction{
int M; // Molecular 分子
int D; // Denominator 分母
};
int main()
{
int m, n;
while(scanf("%d%d", &m, &n))
{
if(m == && n == ) break;
fraction L = {, }, M = {, }, R = {, }; while(){
long double t1 = (long double) m / n, t2 = (long double)M.M / M.D;
if(t1 < t2) {
printf("L");
R = M;
M.M += L.M;
M.D += L.D;
}
else if(t1 > t2) {
printf("R");
L = M;
M.M += R.M;
M.D += R.D;
}
else { printf("\n"); break;}
}
}
return ;
}

递归算法如下:

题目:给你一颗分数组成的二叉树,初始值是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 <iostream>
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 >> m >> n && !(m == && n == ))
tree(, , , , m, n);
return ;
}

uva 10077 - The Stern-Brocot Number System的更多相关文章

  1. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  2. Moduli number system

    A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...

  3. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  4. The Stern-Brocot Number System(排序二进制)

    The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...

  5. POJ 1023 The Fun Number System

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  6. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  7. POJ1023 The Fun Number System

    题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...

  8. UVa 11651 Krypton Number System DP + 矩阵快速幂

    题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...

  9. lightOJ 1172 Krypton Number System(矩阵+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...

随机推荐

  1. How to use PhotoshopApplication in maxscript

    未完待续 ps_app= createOLEObject "Photoshop.Application" ps_app.Load "d:\\test\\aaa.tga&q ...

  2. 基于Karma和Jasmine的AngularJS测试

    1:工程目录结构 y@y:karma-t01$ tree -L 3.├── client│   ├── app│   │   └── user│   ├── bower_components│   │ ...

  3. oracle number 和sqlserver numeric的区别

    number如果不指定范围默认是可以输入所有位数的小数,numeric如果不指定小数默认是不允许输入小数

  4. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. 【转】VS调试技巧

    [转自]http://blog.csdn.net/ghttzsqgm/article/details/5326894 http://blog.csdn.net/cadcisdhht/article/d ...

  6. Types of AOP

    There are two distinct types of AOP: static and dynamic. The difference between them is really the p ...

  7. adb devices: no permissions

    现象: #sudo adb root Error:insufficient permissions for device #adb devices List of devices attached ? ...

  8. C - A Plug for UNIX - poj 1087(最大流)

    题目大意:这个题意有些蛋疼,看了很大会才明白什么意思,有N个插座,这些插座都是有类型的只能给这种类型的电器充电,下面接着给了M种电器,和电器的插头类型,还有K种转换器,可以把一种类型转换成另一种,转换 ...

  9. java排序算法-选择排序

    public class SelectionSort { private static void selectSortTest() { int[] sortArray = { 5, 2, 4, 1, ...

  10. 在网页中在线浏览ppt文档

    方法一: 把ppt文件的扩展名直接修改为pps,嵌入到网页中 缺点:这种方式浏览器会提示是打开,还是下载,选择打开的话会直接在浏览器中打开,并且客户端一定要安装Office PowerPoint才能打 ...