24h購物| | PChome| 登入
2012-01-12 16:42:23| 人氣1,334| 回應0 | 上一篇 | 下一篇

[UVA] 834 - Continued Fractions

推薦 0 收藏 0 轉貼0 訂閱站台


  Continued Fractions 

Let b0, b1, b2,..., bn be integers with bk > 0 for k > 0. The continued fraction of order n with coeficients b1, b2,..., bn and the initial term b0 is defined by the following expression

$displaystyle {frac{1}{b_1 + frac{1}{b_{2 + ldots + frac{1}{b_n}}}}}$
which can be abbreviated as [b0;b1,..., bn].

An example of a continued fraction of order n = 3 is [2;3, 1, 4]. This is equivalent to

$displaystyle {frac{1}{3 + frac{1}{1 + frac{1}{4}}}}$

Write a program that determines the expansion of a given rational number as a continued fraction. To ensure uniqueness, make bn > 1.

Input 

The input consists of an undetermined number of rational numbers. Each rational number is defined by two integers, numerator and denominator.

Output 

For each rational number given in the input, you should output the corresponding continued fraction.

Sample Input 

43 19
1 2

Sample Output 

[2;3,1,4]
[0;2]



作法 : 輾轉相除法

#include<stdio.h>

int main() {
    int x, y;
    while(scanf("%d %d", &x, &y) == 2) {
        int A[32], At = 0, tmp, i;
        while(x%y) {
            A[At++] = x/y;
            tmp = x, x = y, y = tmp%y;
        }
        A[At++] = x;
        printf("[%d", A[0]);
        for(i = 1; i < At; i++) {
            if(i == 1)    printf(";");
            else        printf(",");
            printf("%d", A[i]);
        }
        puts("]");
    }
    return 0;
}

台長: Morris
人氣(1,334) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 382 - Perfection
此分類上一篇:[UVA] 640 - Self Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文