uva 10077 The Stern-Brocot Number System

来源:互联网 发布:论坛网站源码 编辑:程序博客网 时间:2024/04/27 09:01

题目:给你一颗分数组成的二叉树,初始值是1/1,两边的边界分别是0/1与1/0,然后递归建立子树节点,

            每个子树的节点值为两边的边界值得分子之和比上分母之和,新的值也加入边界值。

/*The Stern-Brocot Number SystemUVA, 100772015/9/1721:44*/#include <iostream>using namespace std;struct score{//分数可以考虑结构体long long x;//数据大小也要考虑long long y;};score a,b,c,d;void dfs(){    if(a.x==d.x&&a.y==d.y){        cout<<endl;        return ;    }    else if(a.x *0.1/a.y<d.x *0.1/d.y)    {        cout<<'R';        b.x=a.x;b.y=a.y;        a.x+=c.x;a.y+=c.y;        dfs();    }    else{        cout<<'L';        c.x=a.x;c.y=a.y;        a.x+=b.x;a.y+=b.y;dfs();    }    return ;}int main(){   while(cin>>d.x>>d.y&&d.x>=1&&d.y>=1)   {       a.x=1;a.y=1;       b.x=0;b.y=1;       c.x=1;c.y=0;       if(d.x==1&&d.y==1);/*       考虑切记1和1,不然输出不对       */       else{dfs();       }   }    return 0;}


0 0