编程介的小学生 2020-01-17 21:47 采纳率: 20.5%
浏览 99

Fraction Tree 分形树的问题

Problem Description
Fraction Tree ,alse called Stern-Brocot Tree.It's a beautiful way to construct the set of all nonnegative fractions.The idea is to start with irreducible fractions representing zero and infinity,
1/0 0/1
and then between adjacent fractions n/m and n'/m' we insert fraction (n+n')/ (m+m'), then we obtain
1/0 1/1 0/1
Repeating the process, we get
1/0 2/1 1/1 1/2 0/1
and then
1/0 3/1 2/1 3/2 1/1 2/3 1/2 1/3 0/1
and so forth. It can be proven that every irreducible fraction appears at some iteration and no fraction ever appears twice . The process can be represented graphically:

We can,in fact,regard the Stern-Brocot Tree as a number system for representing rational numbers,because each positive,reduced fractio 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 1/1 down to 1/2,then right to 2/3,then right to 3/4,then left to 5/7.We can consider LRRL to be a representatio of 5/7. Every positive fraction gets represented in this way as a unique string of L's and R's.
There are two natural questios:
(1)Given positive integers m and n (m is coprime with n).what's the string of L's and R's that corresponds to m/n?
(2)Given a string of L's and R's,what fraction corresponds to it?
Now you need to write a problem to solve them.

Input
The first line of input contains a single integer T - a number of test cases.
Each of the next T(T <= 1000) lines begin with a integer K(which kind of probrlem),if K = 1,following two integers M and N(M,N <= 1000000).else following a string of L's and R's(length <= 10).

Output
For each set of data the program prints the answer.

Sample Input
2
1 5 7
2 LRRL

Sample Output
LRRL
5 7

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条