想法:
  初始化三個數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. Altera quartus II遇到的问题

    编译时提示: Warning (13024): Output pins are stuck at VCC or GND Warning (13410): Pin "SCLK" is ...

  2. Solr4.8.0源码分析(10)之Lucene的索引文件(3)

    Solr4.8.0源码分析(10)之Lucene的索引文件(3) 1. .si文件 .si文件存储了段的元数据,主要涉及SegmentInfoFormat.java和Segmentinfo.java这 ...

  3. STM32库中 __IO 修饰符(volatile修饰符)

    STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO原 ...

  4. oracle中的日期加减法

    --加法 ) from dual; --加1年 ) from dual; --加1月 ,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期 ,'yyyy-mm-dd ...

  5. Eclipse里的智能提示

    Eclipse 3.1里的智能提示功能对于写JAVA程序又不记得类名和函数的人来说是一个很好的助手工具,但是Eclipse里的智能提示的快捷键是Ctrl+Space,在中文Windows操作系统中它确 ...

  6. Android应用开发学习笔记之多线程与Handler消息处理机制

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 和JAVA一样,Android下我们可以通过创建一个Thread对象实现多线程.Thread类有多个构造函数,一般通 ...

  7. filter高级应用

    Filter高级应用: Decorator模式  1)包装类需要和被包装对象 实现相同接口,或者继承相同父类 2)包装类需要持有 被包装对象的引用   在包装类中定义成员变量,通过包装类构造方法,传入 ...

  8. 关于oledb对Excel的读取

    这两天项目需求要检索excel的内容,于是就研究了一下,话不多说,我就直接贴代码1.首先是连接excel. public DataTable SearchSheetToDT(string strSea ...

  9. Alert Views

    Alert views display a concise and informative alert message to the user. Alert views convey importan ...

  10. ORACLE 索引概述

    索引是数据库中一种可选的数据结构,她通常与表或簇相关.用户可以在表的一列或数列上建立索引,以提高在此表上执行 SQL 语句的性能.就像本文档的索引可以帮助读者快速定位所需信息一样,Oracle 的索引 ...