Challenge Programming Pc/uva Stern-brocot algebra system

Source: Internet
Author: User

/*stern-brocot algebra System Stern-brocot tree is a wonderful way to generate all non-negative, minimal fraction m/n.         The basic approach is to start with the two fractions (0/1, 1/0) and repeat as needed: Insert (M+M1) between adjacent fractions m/n and m1/n1 (N+N1) For example, you can see it as an infinitely extended two-fork Tree 0 1                           1---------1 1 0/\/\     1 2------2 1/\/\/\/1 2 3 3------------3 3 2 1 ........ By using the letters L and R, respectively, to walk left and right from the root of the tree, a sequence of L and R uniquely determines one of the locations of the trees. The only exception is 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) {Retu RN;} 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&LT;TMP&AMP;&AMP;TMP2&GT;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 7878 3231 1*/

  

Challenge Programming Pc/uva Stern-brocot algebra system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.