题解 | 连分数

连分数

https://www.nowcoder.com/practice/8e225697ad4043b5a2669f890ae90974

#include<bits/stdc++.h>

using namespace std;

int T;
int p,q;

inline string work(int x,int y)
{
    if(x%y == 0) return to_string(x/y);
    int tmp = x/y;
    x %= y;
    if(y%x == 0) return to_string(tmp) + "+1/" + work(y,x);
    else return to_string(tmp) + "+1/" + "{" + work(y,x) +"}";
}

inline void solve()
{
    cin>>p>>q;
    cout<<p<<'/'<<q<<" = "<<work(p,q)<<'\n';
}

int main()
{
    cin>>T;
    while(T--) solve();
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务