The Stern-Brocot Number System

Input: standard input

Output: standard output

The Stern-Brocot tree is a beautiful way for constructing the set of all nonnegative fractions m / n where m and n are relatively prime. The idea is to start with two fractions and
then repeat the following operations as many times as desired:

Insert between two adjacent fractions and .

For example, the first step gives us one new entry between and ,

and the next gives two more:

The next gives four more,

and then we will get 8, 16, and so on. The entire array can be regarded as an infinite binary tree structure whose top levels look like this:

The construction preserves order, and we couldn't possibly get the same fraction in two different places.

We can, in fact, regard the Stern-Brocot tree as a number system for representing rational numbers, because each positive, reduced fraction occurs exactly once. Let's use the letters L and R to
stand for going down to the left or right branch as we proceed from the root of the tree to a particular fraction; then a string of L's and R's uniquely identifies a place in the tree. For example, LRRL means that we go left from down
to , then right to , then right to ,
then left to . We can consider LRRL to be a representation of . Every
positive fraction gets represented in this way as a unique string of L's and R's.

Well, actually there's a slight problem: The fraction corresponds to the empty string, and we need a notation for that. Let's agree
to call it I, because that looks something like 1 and it stands for "identity".

In this problem, given a positive rational fraction, you are expected to represent it in Stern-Brocot number system.

Input

The input file contains multiple test cases. Each test case consists of a line contains two positive integers m and n where m and n are relatively prime. The input terminates with a test case
containing two 1's for m and n, and this case must not be processed.

Output

For each test case in the input file output a line containing the representation of the given fraction in the Stern-Brocot number system.

Sample Input

5 7

878 323

1 1

 

Sample Output

LRRL

RRLRRLRLLLLRLRRR

题目大意:

求出给出数字在每一层树枝上的左边还是右边。

解题思路:

数的左边越来越小,右边越来越大,中间的1是分界点。

模板代码:

#include<iostream>
#include<string>
using namespace std;
///////////////////
struct Fraction{
int m, n;
Fraction(int a = 0, int b = 0){m = a; n = b;}
bool friend operator == (Fraction f1, Fraction f2){
return f1.m*f2.n == f2.m*f1.n;
}
bool friend operator < (Fraction f1, Fraction f2){
return f1.m*f2.n < f2.m*f1.n;
}
};
///////////////
class SBNumber{
private:
Fraction x; // from input
string ans; // for result
public:
bool readCase(){cin >> x.m >> x.n; return (x.m != 1)||(x.n != 1);}
void computing();
void outAns(){cout << ans << endl;}
};
void SBNumber::computing(){
Fraction lt = Fraction(0, 1);
Fraction rt = Fraction(1, 0);
ans.clear();
while(lt < rt){
Fraction mid = Fraction(lt.m + rt.m, lt.n + rt.n);
if(mid == x){
break;
}else if(mid < x){
ans.push_back('R');
lt = mid;
}else{// mid > x
ans.push_back('L');
rt = mid;
}
}
}
int main(){
SBNumber sbn;
while(sbn.readCase()){
sbn.computing();
sbn.outAns();
}
return 0;
}

代码:

#include<iostream>
#include<cstdio>
#include<string> using namespace std; int main(){
int m,n,summ,sumn;
while(cin>>m>>n&&(m!=1||n!=1)){
string ans;
int m0=0,m1=1;
int n0=1,n1=0;
while(1){
summ=m0+m1;
sumn=n0+n1;
int temp=m*sumn-n*summ;
if(temp>0){
ans+='R';
m0=summ;
n0=sumn;
}
else if(temp==0) break;
else{
ans+='L';
m1=summ;
n1=sumn;
}
}
cout << ans << endl;
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

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. 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 ...

  3. POJ1023 The Fun Number System

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

  4. Moduli number system

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

  5. 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 ...

  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. [CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示

    5.2 Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary ...

  8. PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...

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

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

随机推荐

  1. 《数字图像处理原理与实践(MATLAB文本)》书代码Part7

    这篇文章是<数字图像处理原理与实践(MATLAB文本)>一本书的代码系列Part7(由于调整先前宣布订单,请读者注意分页程序,而不仅仅是基于标题数的一系列文章),第一本书特色186经225 ...

  2. 开源Math.NET基础数学类库使用(02)矩阵向量计算

    原文:[原创]开源Math.NET基础数学类库使用(02)矩阵向量计算 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math.NET(一)综合介绍    ...

  3. Android应用开发:LoaderManager在Activity/Fragment中的使用分析

    LoaderManager 外部接口initLoader:起始 public <D> Loader<D> initLoader(int id, Bundle args, Loa ...

  4. [ACM] HDU 1227 Fast Food (经典Dp)

    Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. Paypal-Express Checkout快捷支付方式的android端开发心得(二)

    一.前导 上一篇讲的不是非常好,这里再又一次讲一下. Paypal手机支付有2种形式: 1.Mobile Express Checkout,MEC,快捷支付 2.MPL 假设採用MEC支付方式,这样的 ...

  6. Java 新特性(3) - JDK7 新特性

    http://www.ibm.com/developerworks/cn/java/j-lo-jdk7-1/ JSR292:支持动态类型语言(InvokeDynamic) 近 年来越来越多的基于 JV ...

  7. CCFadeOut ,CCFadeIn 不能使用的原因

    CCFadeOut *action = CCFadeOut::create(0.5f);  image->runAction(action); 截取部分代码.以上是我写游戏时候遇到的问题代码, ...

  8. HDU3549 Flow Problem 【最大流量】

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  9. ASP.NET Identity

    使用ASP.NET Identity实现基于声明的授权 阅读目录 走进声明的世界 创建并使用声明 基于声明的授权 使用第三方来身份验证 小节 在这篇文章中,我将继续ASP.NET Identity 之 ...

  10. 一个人ACM(我们赶上了ACM)

    时间过得真快,不经意间我已经花了两年的大学生活,现在是时候写的东西.纪念馆两年左右的时间,最近一直在玩博客.我写了一个博客.纪念我们终将逝去的青春. 就从报考说起吧.高考成绩一般,自己选择了土建类的学 ...