/*
Stern-Brocot代数系统 Stern-Brocot树是一种生成所有非负的最简分数m/n的美妙方式。
其基本方式是从(0/1, 1/0)这两个分数开始, 根据需要反复执行如下操作:
在相邻的分数 m/n 和 m1/n1之间插入 (m+m1)/(n+n1) 例如,可以将它看成是无限延伸的二叉树
0 1 1
--- --- ---
1 1 0
/ \
/ \
1 2
--- ---
2 1
/ \ / \
/ \ / \
1 2 3 3
--- --- --- ---
3 3 2 1
.... ... .......
用字母 L 和 R 分别表示从树根开始的一步 往左走 和 往右走,
则一个L和R组成的序列唯一确定了树中的一个位置。 唯一例外的是 1/1.
*/ #include<iostream>
#include<string>
#include<vector>
using namespace std; typedef struct Point
{
int x;
int y;
const Point& operator+=(const Point& pt)
{
x+=pt.x;
y+=pt.y;
return *this;
}
}LP,RP; void getStr(LP leftP, RP rightP, Point pt, const Point P, string& str)
{
if(pt.x == P.x && pt.y==P.y)
{
return;
}
double tmp = pt.x*1.0/pt.y;
double tmp1 = leftP.x*1.0/leftP.y;
double tmp2 = P.x*1.0/P.y;
double tmp3;
if(rightP.y==0)
{
tmp3 = INT_MAX*0.1;
}
else
{
tmp3 = rightP.x*1.0/rightP.y;
}
if(tmp2<tmp&&tmp2>tmp1)
{
rightP = pt;
pt+=leftP; str+='L';
getStr(leftP, rightP, pt, P, str);
}
if(tmp2>tmp&&tmp2<tmp3)
{
leftP = pt;
pt+=rightP; str+='R';
getStr(leftP, rightP, pt, P, str);
}
} int main()
{
int M, N;
vector<string> svec;
string str; Point leftP, rightP, Pt, P;
while(cin>>M>>N)
{
if(1==M&&1==N)
break;
str.clear();
P.x = M;
P.y = N;
leftP.x = 0;
leftP.y = 1;
rightP.x = 1;
rightP.y = 0;
Pt.x = 1;
Pt.y = 1;
getStr(leftP, rightP, Pt, P, str);
svec.push_back(str);
} for (int i=0; i<svec.size(); i++)
{
cout<<svec[i]<<endl;
} return 0;
} /*
5 7
878 323
1 1 */

  

挑战编程PC/UVa Stern-Brocot代数系统的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  3. CSDN挑战编程——《数学问题》

    数学问题 题目详情: 给你两个长度为n的正整数序列分别为{a1,a2,a3...an},{b1,b2,b3...bn},0<ai,bi<=100: 设S=max{x1*a1+x2*a2+x ...

  4. CSDN挑战编程——《绝对值最小》

    绝对值最小 题目详情: 给你一个数组A[n],请你计算出ans=min(|A[i]+A[j]|)(0<=i,j<n). 比如:A={1, 4, -3}, 则: |A[0] + A[0]| ...

  5. 挑战编程 uva100 3n+1

    挑战编程 刘汝佳 的第一道习题  热身题 熟悉下提交格式 题意 #include <iostream> #include <algorithm> using namespace ...

  6. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版

    , '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...

  7. The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  8. CSDN挑战编程——《金色十月线上编程比赛第二题:解密》

    金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...

  9. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

随机推荐

  1. Windows下64位Apache服务器的安装

    转自:http://www.blogjava.net/greatyuqing/archive/2013/02/13/395308.html 首先需要说明的是,Apaceh服务器没有官方的64位版本,只 ...

  2. 如何写出安全的API接口

    通过园友们的讨论,以及我自己查了些资料,然后对接口安全做一个相对完善的总结,承诺给大家写个demo,今天一并放出. 对于安全也是相对的,下面我来根据安全级别分析 1.完全开放的接口 有没有这样的接口, ...

  3. 解析Cloudera Manager内部结构、功能包括配置文件、目录位置等

    转载自 http://www.aboutyun.com/thread-9189-1-1.html 问题导读1.CM的安装目录在什么位置? 2.hadoop配置文件在什么位置? 3.Cloudera m ...

  4. mysql一次插入多条数据

    mysql一次插入多条数据: INSERT INTO hk_test(username, passwd) VALUES ('qmf2', 'qmf2'),('qmf3', 'qmf3'),('qmf4 ...

  5. NGUI全面实践教程(大学霸内部资料)

    NGUI全面实践教程(大学霸内部资料)   试读文档下载地址:链接:http://pan.baidu.com/s/1jGosC9g 密码:8jq5 介绍:NGUI全面实践教程(大学霸内部资料)本书是国 ...

  6. Animator窗口视图Project视图PlayerIdleAnimation和PlayerWalkingAnimation

    Animator窗口视图Project视图PlayerIdleAnimation和PlayerWalkingAnimation 通过上一小节的操作,我们新建了2个动画:PlayerIdleAnimat ...

  7. 简单几何(四边形形状) UVA 11800 Determine the Shape

    题目传送门 题意:给了四个点,判断能构成什么图形,有优先规则 分析:正方形和矩形按照点积为0和长度判断,菱形和平行四边形按向量相等和长度判断,梯形按照叉积为0判平行.因为四个点是任意给出的,首先要进行 ...

  8. BZOJ3830 : [Poi2014]Freight

    首先为了保证发车时间都不同,T[i]=max(T[i],T[i-1]+1) 然后设f[i]表示前i列车回来的最早时刻 f[i]=min(max(T[i],f[j]+i-j-1)+s*2+i-j-1) ...

  9. Windows Phone 硬件检测

    private static bool IsWvga{ get { return App.Current.Host.Content.ScaleFactor == 100; }} private sta ...

  10. BJOI2015 Day2

    轮到罗剑桥出题了 这是什么风格,中文名称与英文名称分明对不上吗233 T1: 似乎只会做这道题23333 A....BE ........ C....DF 据题意数学变形得A-C<=B-D,B- ...