• UVa834 Continued Fractions【连分数】


    问题链接UVa834 Continued Fractions

    问题简述:参见上述链接。

    问题分析

    这是一个分数转换为连分数的问题。

    需要了解和掌握有关连分数的概念与表示方式,可以查看中英文版的维基百科的“连分数”。

    剩下的就是简单的分数计算问题了。

    程序说明

    需要考虑特例的情况,例如,分数实际上是整数的情况。

    程序中不考虑约分的问题。

    程序中的迭代计算过程尽可能地简洁。

    题记把功能封装到函数是一种好的做法,程序逻辑更加简洁。


    AC的C++语言程序如下:

    /* UVa834 Continued Fractions */
    
    #include <iostream>
    
    using namespace std;
    
    void fraction2continuedfraction(int n, int d)
    {
        int nextd;
        char separator = ';';
    
        if(n % d == 0)
            printf("[%d]
    ", n / d);
        else {
            printf("[");
            printf("%d", n / d);
            nextd = n % d;
            n = d;
            d = nextd;
            while(d != 0) {
                printf("%c", separator);
                printf("%d", n / d);
    
                nextd = n % d;
                n = d;
                d = nextd;
    
                separator = ',';
            }
            printf("]
    ");
        }
    }
    
    int main()
    {
        int n, d;
    
        while(cin >> n >> d) {
            fraction2continuedfraction(n, d);
        }
    
        return 0;
    }


  • 相关阅读:
    友链
    P2572 [SCOI2010]序列操作
    「THP3考前信心赛」解题报告
    DP中的树上边/点覆盖问题
    P3413 SAC#1
    luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers
    睿智错误
    常见套路?
    奇怪的点子
    最近做过一些比较好的题
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563749.html
Copyright © 2020-2023  润新知