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;
}


原文地址:https://www.cnblogs.com/tigerisland/p/7563749.html