码迷,mamicode.com
首页 > 其他好文 > 详细

The Stern-Brocot Number System(树的左边还是右边)

时间:2014-09-19 10:16:15      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:uva



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 bubuko.com,布布扣and then repeat the following operations as many times as desired:

Insert bubuko.com,布布扣between two adjacent fractions bubuko.com,布布扣and bubuko.com,布布扣.

For example, the first step gives us one new entry between bubuko.com,布布扣and bubuko.com,布布扣,

bubuko.com,布布扣

and the next gives two more:

bubuko.com,布布扣

The next gives four more,

bubuko.com,布布扣

 

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:

 

bubuko.com,布布扣

 

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 bubuko.com,布布扣down to bubuko.com,布布扣, then right to bubuko.com,布布扣, then right to bubuko.com,布布扣, then left to bubuko.com,布布扣. We can consider LRRL to be a representation of bubuko.com,布布扣. 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 bubuko.com,布布扣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<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(树的左边还是右边)

标签:uva

原文地址:http://blog.csdn.net/hush_lei/article/details/39386699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!