/*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<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 7878 3231 1*/
Challenge Programming Pc/uva Stern-brocot algebra system