The Stern-Brocot Number System(排序二叉树)
来源:互联网 发布:php身份证验证接口 编辑:程序博客网 时间:2024/04/27 04:50
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 7878 323
1 1
Sample Output
LRRLRRLRRLRLLLLRLRRR
题目大意:
求出给出数字在每一层树枝上的左边还是右边。
解题思路:
数的左边越来越小,右边越来越大,中间的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 resultpublic: 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(排序二叉树)
- UVa Problem 10077 The Stern-Brocot Number System (Stern-Brocot 代数系统)
- (挑战编程_5_7)The Stern-Brocot Number System
- 10077 - The Stern-Brocot Number System
- 110507 The Stern-Brocot Number System
- UVa 10077 - The Stern-Brocot Number System
- uva 10077 The Stern-Brocot Number System
- UVA 10077 The Stern-Brocot Number System
- UVa Problem Solution: 10077 - The Stern-Brocot Number System
- UVa 10077 The Stern-Brocot Number System (数学)
- Hdu 6209 The Intersection(Stern-Brocot tree+二分)
- Farey序列与Stern-Brocot树
- Farey序列与Stern-Brocot树
- Stern-Brocot树 (生成0-1之间的所有真分数)
- hdu 4556 Stern-Brocot
- hdu4556-Stern-Brocot Tree
- hdu4556-Stern-Brocot Tree
- Farey序列(Stern-Brocot tree的衍生)
- Java 之 不要问我从哪里来 《初识Java》
- Ueditor图片上传设置(1.4.3 JSP版本) - 解决图片不能显示问题
- 黑马程序员------银行业务调度系统的设计
- 【学习17】Box2D创建物体和碰撞检测
- Scroller的应用--滑屏实现
- The Stern-Brocot Number System(排序二叉树)
- cocos2d-x基本知识点:按键事件
- android自定义控件(四) View中的方法
- K&R_6.5用二叉树统计单词出现的次数
- 交换两个变量的值,不使用第三个变量的四种法方
- 黑马程序员——交通灯管理系统
- 封装一下curl,支持设置refer,post数据,user-agent等
- 基于Linux的火焰识别系统的实现
- 要在win7上装个苹果操作系统