problem link:- Stern-Brocot Tree
submit link:- Submit
code ref:- c++ language, bfs-tree
#include<bits/stdc++.h>
using namespace std;
int main()
{
int test;
cin>>test;
while(test--)
{
string s;
long long ld=0,ln=1,rd=1,rn=0,x=1,y=1;
cin>>s;
for(int i=0;i<(int)s.size();i++)
{
if(s[i]=='R'){
ld = x;
ln = y;
x=ld+rd;
y=ln+rn;
}
else{
rd = x;
rn = y;
x=ld+rd;
y=ln+rn;
}
}
cout<<x<<'/'<<y<<endl;
}
return 0;
}
Please Don't copy without understanding.
This code is for your logical help not for copying.
No comments:
Post a Comment