uva 10077 - The Stern-Brocot Number System
想法:
初始化三個數L=0/1, M=1/1, R=1/0,設輸入的分數為a:
- 如果a<M,那麼要往左邊走,
R = M;
M = (L分子+M分子)/(L分母+M分母); - 如果a>M,往右邊走,
L = M;
M = (R分子+M分子)/(R分母+M分母); - 如果a==M,停止。
#include <cstdio>
using namespace std;
struct fraction{
int M; // Molecular 分子
int D; // Denominator 分母
};
int main()
{
int m, n;
while(scanf("%d%d", &m, &n))
{
if(m == && n == ) break;
fraction L = {, }, M = {, }, R = {, }; while(){
long double t1 = (long double) m / n, t2 = (long double)M.M / M.D;
if(t1 < t2) {
printf("L");
R = M;
M.M += L.M;
M.D += L.D;
}
else if(t1 > t2) {
printf("R");
L = M;
M.M += R.M;
M.D += R.D;
}
else { printf("\n"); break;}
}
}
return ;
}
递归算法如下:
题目:给你一颗分数组成的二叉树,初始值是1/1,两边的边界分别是0/1与1/0,然后递归建立子树节点,
每个子树的节点值为两边的边界值得分子之和比上分母之和,新的值也加入边界值。
分析:递归,数据结构。看到上面的描述就可以做了吧,直接递归。
tree(L, R, key) {
if(add(L+R)= key)return;
if(add(L+R)< key){
cout << "R";
tree(L,add(L,R),key);
}else {
cout << "L";
tree(add(L,R),R,key);
}
}
说明:强大的递归╮(╯▽╰)╭。
#include <iostream>
using namespace std; void tree(int Lx, int Ly, int Rx, int Ry, int Tx, int Ty)
{
if (Lx+Rx == Tx && Ly+Ry == Ty) {
printf("\n");
return;
}
if ((Lx+Rx)*Ty < (Ly+Ry)*Tx) {
printf("R");
tree(Lx+Rx, Ly+Ry, Rx, Ry, Tx, Ty);
}else {
printf("L");
tree(Lx, Ly, Lx+Rx, Ly+Ry, Tx, Ty);
}
} int main()
{
int n,m;
while (cin >> m >> n && !(m == && n == ))
tree(, , , , m, n);
return ;
}
uva 10077 - 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 ...
- 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 ...
- The Stern-Brocot Number System(排序二进制)
The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...
- 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 ...
- 为什么实数系里不存在最小正数?(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 ...
- POJ1023 The Fun Number System
题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...
- UVa 11651 Krypton Number System DP + 矩阵快速幂
题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...
- lightOJ 1172 Krypton Number System(矩阵+DP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...
随机推荐
- perl /m
<pre name="code" class="html">[root@backoffice01 ~]# cat a1.pl my $_=" ...
- 关于Cookie和Session【转载】
当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set-Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSES ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- jQuery Ajax全解析
jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 我们先来看一些简单的方法,这些方法都是对jQuery.ajax( ...
- 数据库 —— 基于 ORM 模型的 Hibernate 的使用(java)
目录: 使用Hibernate Tool 从数据库中生成对应的实体类 1.使用Hibernate Tool 从数据库中生成对应的实体类 Title:Using Hibernate Tools gene ...
- [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
// backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _Ann ...
- 使用symbolicatecrash分析crash文件
对于我们iOS开发者来说,最心碎的事莫过于苹果审核一个星期后上架app store,而第二天就报出闪退bug.一周前我刚经历过,而且最坑的是由于第一次做个人开发,经验不足,没有集成友盟的分析SDK,还 ...
- [转] java中注解的使用与实例
注解目前非常的流行,很多主流框架都支持注解,而且自己编写代码的时候也会尽量的去用注解,一时方便,而是代码更加简洁. 注解的语法比较简单,除了@符号的使用之外,它基本与Java固有语法一致.Java S ...
- NYOJ 116士兵杀敌(二) 树状数组
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...
- Interpolator 插值器
简介 Interpolator:撺改者,校对机,分类机,插补器 Interpolator 定义了动画的变化速度,可以实现匀速.正加速.负加速.无规则变加速等,这使得基本的动画得以实现加速.减速等效果. ...