想法:
  初始化三個數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. STM32f103------按键处理

    (1)按键去抖 /******************************************函数名称:Key_Scan(GPIO_TypeDef*GPIOx,u16 GPIO_pin)*描 ...

  2. angular2 学习笔记 ( DI 依赖注入 )

    refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...

  3. poj 3026Borg Maze

    http://poj.org/problem?id=3026 #include<cstdio> #include<iostream> #include<cstring&g ...

  4. -_-#【Better JS Code】插入迭代值

    +function() { var i = 0 var values = ['xiaoming', 'xiaohong'] var name = values[i] i++ console.log(n ...

  5. 【转】Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/10163203 转载请注明出处:http://blog.csdn.net/xiaanming/ ...

  6. 【动态规划】XMU 1029 矩阵链乘法

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1029 题目大意: 题同乘法难题.给n+1个数,头尾不能动,中间的数可取出,取出时代价是 ...

  7. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  8. 《SDN核心技术剖析和实战指南》3.1控制器核心技术读书笔记

    在SDN的架构中,控制器可以说是SDN的核心,它负责对底层转发设备的控制以及向上层应用提供可编程性的北向接口.从实现上看,主要分三个层面来考虑,南向接口技术,北向接口技术以及东西向的可扩展性能力.下面 ...

  9. Python学习打算

    背景:本人用python写自动化脚本,基础的东西都会啦.但是呢,鉴于年轻应该好好学习,所以打算再买一本python书籍用来精读. Python 好的博客: Python快速教程(好多,一点也不快速): ...

  10. cocos日记

    n/2/2015 ADT       E:\All Setup\eclipse_past\Eclipse_plugins_ADT 15/2/2015 配置cocos win32 开发环境 链接:htt ...