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)至少包 ...
随机推荐
- chrome使用技巧(转)good
阅读目录 写在前面 快速切换文件 在源代码中搜索 在源代码中快速跳转到指定的行 使用多个插入符进行选择 设备模式 设备传感仿真 格式化凌乱的js源码 颜色选择器 改变颜色格式 强制改变元素状态(方便查 ...
- 开源通讯组件ec
跨平台开源通讯组件elastic communication elastic communication是基于c#开发支持.net和mono的通讯组件(简称EC),EC的主要目的简化mono和.net ...
- VirtualBox创建虚拟电脑、执行Genymotion模拟器报错
当安装完Genynition关于Android应用的调试模拟器之后,在Genymotion执行的平台virtualBox:VirtualBox创建虚拟电脑.执行Genymotion模拟器报错: 错误卖 ...
- V离MWare至Openstack至FDIO
离VMWare至Openstack 至FDIO --软件虚拟化和硬件虚拟化相结合 作者:廖恒 以VMWare为代表的软件虚拟化技术在企业IT中已是耳熟能详的不争现实.据在HPISS任职的好友告知,V ...
- iOS 自己主动布局教程
springs和struts的问题 你肯定非常熟悉autosizing masks-也被觉得是springs&struts模式.autosizing mask决定了当一个视图的父视图大小改变时 ...
- 在Repeater控件中使用if语句
原文:在Repeater控件中使用if语句 .Afr_ARTICLE_TITLE { font: NORMAL BOLD 14px "Tahoma"; } .Afr_CONTENT ...
- MySQL列:innodb的源代码的分析的基础数据结构
在过去的一年中的数据库相关的源代码分析.前段时间分析levelDB实施和BeansDB实现,数据库网络分析这两篇文章非常多.他们也比较深比较分析,所以没有必要重复很多劳力.MYSQL,当然主要还是数据 ...
- 【从零学习openCV】IOS7根据人脸检测
前言: 人脸检測与识别一直是计算机视觉领域一大热门研究方向,并且也从安全监控等工业级的应用扩展到了手机移动端的app.总之随着人脸识别技术获得突破,其应用前景和市场价值都是不可估量的,眼下在学习ope ...
- Appium - iOS Mac环境结构
Appium - iOS Mac环境结构 笔者: Max.Bai 时间: 2014/10 1. iOS开发环境的搭建 1.1系统要求 MacOS X 10.7 or higher, 10.9.2 re ...
- 4.4、Libgdx用法查询执行环境相关性
(原版的:http://www.libgdx.cn/topic/46/4-4-libgdx%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E6%9F%A5%E8%AF%A2% ...