/*
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. js 默认行为取消

    js 默认行为取消   可以简单的  return false;   看需求吧 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transit ...

  2. android 按钮宽度按比例

    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_paren ...

  3. Python 中文Key 报错问题

    例如: # -*- coding=UTF-8 -*- import json data = [] with open('data.json') as f: for line in f: data.ap ...

  4. DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)

    题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...

  5. BZOJ3461 : Jry的时间表

    fl[i]表示[1,i]操作一次,且在[j+1,i]处操作的最大值 1:把[j+1,i]改为b[i]: max(sum[j]+b[i]*(i-j)) =b[i]*i+max(-j*b[i]+sum[j ...

  6. 升级到WP8必需知道的13个特性

    http://www.cnblogs.com/sonic1abc/archive/2012/11/28/2792467.html   Windows phone 8 SDK 已经发布一段时间了, 已经 ...

  7. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...

  8. COJ966 WZJ的数据结构(负三十四)

    WZJ的数据结构(负三十四) 难度级别:C: 运行时间限制:20000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给一棵n个节点的树,请对于形如"u  ...

  9. ARC指南2 - ARC的开启和禁止

    要想将非ARC的代码转换为ARC的代码,大概有2种方式: 1.使用Xcode的自动转换工具 2.手动设置某些文件支持ARC 一.Xcode的自动转换工具 Xcode带了一个自动转换工具,可以将旧的源代 ...

  10. 8.20 css样式表:样式分类,选择器。样式属性,

    样式表分类: 1.内联样式表, 和html联合显示,例:<p style="font-size:14px;">内联样式表</p> 2.内嵌样式表 作为一个独 ...