The Stern-Brocot Number System(排序二进制)
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(排序二进制)的更多相关文章
- 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 ...
- 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 ...
- POJ1023 The Fun Number System
题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...
- Moduli number system
A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...
- 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 ...
- 为什么实数系里不存在最小正数?(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 ...
- [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 ...
- PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- lightOJ 1172 Krypton Number System(矩阵+DP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...
随机推荐
- 【OpenCV新手教程之十八】OpenCV仿射变换 & SURF特征点描写叙述合辑
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/33320997 作者:毛星云(浅墨) ...
- 【转】static_cast和reinterpret_cast
static_cast和reinterpret_cast揭秘 收藏 本文讨论static_cast<> 和 reinterpret_cast<>. reinterpret_ca ...
- Python日志输出格式和时间格式
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H:%M:% ...
- Windows创建的基本含义和进程的进程的内核
过程 1 这意味着过程: 1.1 一个是在操作系统的内核对象管理处理. 的统计信息的地方. 1.2 还有一个是地址空间.它包括全部可运行模块或DL L 模块的代码和数据.它还包括动态内存分配的 ...
- ORA-01791: not a SELECTed expression 一种是不 bug 的 bug!
[ora11@lixora ~]$ !sql sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 27 09: ...
- iPhone&iPad DFU及恢复模式刷机、降级教程
再次提醒,刷机需慎重处理. http://blog.csdn.net/ztp800201/article/details/11980643 iphone一共同拥有三种工作模式,各自是正常模式,恢复模式 ...
- C# 闭包问题
C# 闭包问题-你被”坑“过吗? 引言 闭包是什么?以前看面试题的时候才发现这个名词. 闭包在实际项目中会有什么问题?现在就让我们一起来看下这个不太熟悉的名词. 如果在实际工作中用到了匿名函数和lam ...
- JavaScript 初识Promise 对象
什么是Promise? 其实, Promise就是一个类,而且这个类已经成为ES6的标准,是 ECMAScript 6 规范的重要特性之一.这个类目前在chrome32.Opera19.Firefox ...
- 设计模式 Template Method模式 显示程序猿的一天
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/26276093 不断设计模式~ Template Method模式 老套路,看高清 ...
- 使用更清晰DebugLog开发和调试工具
在开发和应用的开发和调试过程中难免会发现故障的过程中.我相信很多做iOS开发程序员Xcode的debug调试功能大加关注. 但在这样做Android开发过程中,却不那么方便,虽然IDE也提供了debu ...