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)至少包 ...
随机推荐
- Simple Automated Backups for MongoDB Replica Sets
There are a bunch of different methods you can use to back up your MongoDB data, but if you want to ...
- node.js抓取数据(fake小爬虫)
在node.js中,有了 cheerio 模块.request 模块,抓取特定URL页面的数据已经非常方便. 一个简单的就如下 var request = require('request'); va ...
- 从jdbc到mybatis
前面我已经写了几篇文章介绍mybatis的使用方法, 现准备从原理上分析mybatis, 本篇将会解说JDBC演变到mybatis的过程. JDBC查询 使用jdbc查询数据库一般有下面七个步骤: 1 ...
- Android截图
Android截图很好的实现,从文档的发展,查看View有一个接口getDrawingCache(),这个接口可以得到View当调用这个接口的位图图像Bitmap. 抓取截图View在图像的某一个时刻 ...
- 苹果WatchKit轻松入门
背景 前段时间苹果Apple推出 WatchKit,用于开发Apple Watch应用,同时也推出了 Xcode6.2 Beta(非稳定版,好期待稳定版)版本用于开发 Watch App.Apple ...
- 【Android进阶】Activity和Fragement中onSaveInstanceState()的使用详解
在activity(或者是fragement)被杀掉之前调用保存每个实例的状态,以保证该状态可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (传 ...
- JDK5什么是新的堵塞队列线程(四)
一. 堵塞队列与普通队列: 队列是一种主要的数据类型,其典型特征是先进先出. 堵塞队列和普通队列的差别在于: 当队列为空时.从队列中获取元素的线程会被堵塞.直到其它的线程往空的队列里插入新的元素: 当 ...
- 为什么OC语言很难
作为一个Objective-C的coder,我总能听到一部分人在这门语言上抱怨有很多问题.他们总在想快速学习这门语言来写一个App出来,但他们也总是联想到Objective-C看上去实在太难了或者在想 ...
- curl转让query string逃生参数
假设curl访问http网站.传递参数.需要使用\如&字首. 例: http://myjenkins/job/run_schedule/buildWithParameters?token=fe ...
- 使用Java快速实现进度条(转)
基于有人问到怎样做进度条,下面给个简单的做法: 主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax. ...