UVa 10077 - The Stern-Brocot Number System



#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <bitset>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef std::vector<int> vi;
typedef std::vector<pair<int, int> > vii;
#define FOR(l) for(vi::iterator it=l.begin();it!=l.end();it++)
#define FOR_L(l, s, e) for(vi::iterator it=l.begin()+s;it!=l.end()-e;it++)

//----------Main source code -----------------//
int main() {
 int m,n;
 double num;
 while(scanf("%d %d", &m, &n)!=EOF&&(m!=1||n!=1)){
  num=(double)m/n;
  string o="";
  double mid=1;
  int fn, fd, ln, ld, mn=1, md=1;
  fn=0;fd=1;ln=1;ld=0;
  while(!(m==mn&&n==md)){
   mn=fn+ln;
   md=ld+fd;
   mid=(double)mn/md;
   if(num>mid) fn=mn, fd=md, o+="R";
   else if(num<mid) ln=mn, ld=md, o+="L";
  }
  cout<<o<<endl;
 }
 return 0;
 
}
Runtime:0.019
Ideone: http://ideone.com/nq1jJR

Comments

Popular Posts