想法:
  初始化三個數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. SPSS基础操作

    1.数据--排序个案(按行排序),排序变量是按列排序2.现有一文件两列,销售地区(字符串或者数字)和业绩,想按地区划分文件,可以排序个案(按销售地区),也可以选择“数据--拆分文件”(注意还是在一个文 ...

  2. 【HDOJ】2492 Ping pong

    线段树+离散化. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 2000 ...

  3. OBJ-C

    1.直接赋值 NSString *name = @"Starain"; 2.用已经存在的字符串进行初始化 NSString *name2 = [NSString stringWit ...

  4. 剑指offer---3

    1.反转单链表,输入链表的头节点,输出该链表,并输出反转后的头节点 这个题目不用再说了,写过N边了 SLnode reverse(SLnode head) { SLnode reverse_head ...

  5. 如何安装Windows 8系统中的telnet组件

    知识点分析:Window 8 系统中Telnet没有默认安装,成为了一个可选组件,“启用或关闭Windows功能”下即可添加此组件. 操作步骤:1.系统桌面下同时按住键盘上 “Windows” 和“X ...

  6. [oracle安装错误处理]ORA-00600: [keltnfy-ldmInit][46], [1], []

    原博文:http://blog.itpub.net/519536/viewspace-614893/ 今天在部署一套10g Oracle(10.2.0.3版本)的过程中,偶遇ORA-00600: in ...

  7. Asp.Net读取服务器EXE文件的方法!(超简单实例)

    Asp.Net读取服务器EXE文件的方法!(超简单实例) Process process = new Process(); process.StartInfo.FileName = "d:\ ...

  8. win8.1 Dism 应用实例

    使用win8.1安装镜像制作wimboot启动 制作wim启动映像文件 Dism /Export-Image /WIMBoot/SourceImageFile:z:\sources\install.w ...

  9. UITextView ios7

    UITextView *textView2 = [[UITextView alloc]initWithFrame:CGRectMake(, textView1.frame.size.height + ...

  10. 将Apache添加为Linux的服务 实现自启动(转)

    在Linux下用源代码方式编译安装完Apache后,启动关闭Apache可以通过如下命令实现: /app/apache2.2.14/bin/apachectl start | stop | resta ...