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. 杭电1171 Big Event in HDU(母函数+多重背包解法)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. Netty+Tomcat热部署端口占用解决办法(转)

    在eclipse使用maven deploy (tomcat:deploy) 热部署netty项目 ,项目启动的时候会报错端口被占用. java.net.BindException: Address  ...

  3. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

  4. 【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)

    Cocos Code IDE 是一个基于 Eclipse 的跨平台 IDE ,专门为 cocos2d lua & js 开发者准备,通过此工具.你能够方便的创建游戏project.编写而且调试 ...

  5. 顺序表----java实现

    最简单的数据结构--顺序表,此处以数组为例. 顺序表的优点:支持随机读取,内存空间利用率高. 顺序表的缺点:1.需要预先给出最大数据元素个数,这往往很难实现. 2.插入和删除时需要移动大量数据. Se ...

  6. hive内置函数大全

    ====================================== 一.关系函数 1.等值比較:=     语法:A=B 操作类型:全部基本类型 2.不等值比較:<>     语 ...

  7. 小谷的战斗Jquery(三)--水平和垂直菜单

    日薪的例子似乎有点低,今天做多.行,这种实现是一个简单的菜单,Web项目中,有两个共同的菜单:纵向和横向.说到从垂直,看原代码. html代码实现最主要的菜单与子菜单 <span style=& ...

  8. Eamcs ditaa基于字符图形产生的图像上

    ditta和artist mode这是一个好兄弟.artist mode帮我创建一个字符模式速度,ditta是java计划,字符图形可被读取,并生成图像. ditta网站:http://ditaa.s ...

  9. ADN中国队参加微软Kinect他赢得了全国比赛三等奖,我们的创意项目与团队Kinect于Naviswork虚拟之旅

    以下是我的英语写了一个简短的总结,直接贴出来. 让我们知道我们在这参加Hackathon That's an exciting Hackathon for me and also China team ...

  10. Java线(一个):线程安全的和不安全

    当我们看JDK API什么时候,总是找一些类描述说:,线程安全或线程安全,例如StringBuilder在,么一句,"将StringBuilder 的实例用于多个线程是不安全的.假设须要这种 ...