编程介的小学生 2017-02-09 08:18 采纳率: 20.5%
浏览 1022
已采纳

Continuous Fractions

Description

A simple continuous fraction has the form:

where the ai’s are integer numbers.

The previous continuous fraction could be noted as [a1, a2, …, an]. It is not difficult to show that any rational number p⁄q, with integers p > q > 0, can be represented in a unique way by a simple continuous fraction with n terms, such that p⁄q = [a1, a2, …, an−1, 1], where n and the ai’s are positive natural numbers.

Your task is to find and print the simple continuous fraction that corresponds to a given rational number.

Input

Input will consist of a series of cases, each one in a line. A line describing a case contains p and q, two integer numbers separated by a space, with 1020 > p > q > 0.

The end of the input is indicated by a line containing 0 0.

Output

Cases must be analyzed in the order that are read from the input. Output for each case will consist of several lines. The first line indicates the case number, starting at 1, using the format:

Case i:

replacing i by the corresponding case number. The second line displays the input data in the form p / q.

The remaining lines must contain the continuous fraction corresponding to the rational number, p⁄q, specified in the given input line. The continuous fraction must be printed accordingly to the following rules:

Horizontal bars are formed by sequences of dashes ‘-’.
The width of each horizontal bar is exactly equal to the width of the denominator under it.
Blank characters should be printed using periods ‘.’.
The number on a fraction numerator must be printed center justified. That is, the number of spaces at either side must be same, if possible; in other case, one more space must be added at the right side.

Sample Input

75 34
65 60
0 0

Sample Output

Case 1:
75 / 34
..........1......
2.+.-------------
............1....
....4.+.---------
..............1..
........1.+.-----
................1
............5.+.-
................1
Case 2:
65 / 60
......1...
1.+.------
.........1
....11.+.-
.........1

  • 写回答

2条回答 默认 最新

  • devmiao 2017-02-13 18:05
    关注
     #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    typedef long long ll;
    const int MAX = 100000;
    const int LEN = 100000;
    char buffer[MAX];
    ll a[MAX];
    ll space[LEN],vin[LEN],bit[LEN];
    
    static ll BIT[18] = {-1,9LL,99LL,999LL,9999LL,99999LL,999999LL,9999999LL,99999999LL,999999999LL,9999999999LL,99999999999LL,999999999999LL,
                9999999999999LL,99999999999999LL,999999999999999LL};
    inline int getBit(const ll& x) {
            for (int i = 0; i < 18; ++i) {
                    if (x <= BIT[i]) return i;
            }
            return 18;
    }
    
    int main() {
        //freopen("C.out", "w", stdout);
        while (~scanf(" %s", buffer)) {
            int len = strlen(buffer);
            int deep = 0, cur;
                    if (buffer[0] == 'L') a[deep++] = 0;
            cur = 1;
            for (int i = 1; i < len; ++i) {
                            if (buffer[i] == buffer[i-1]) {
                                    ++cur;
                            } else {
                                    a[deep++] = cur;
                                    cur = 1;
                            }
            }
                    a[deep] = ++cur;
                    if (deep == 0) {
                printf("%I64d\n", a[0]);
                continue;
                    }
                    for (int i = 0; i <= deep; ++i) {
                            bit[i] = getBit(a[i]);
                    }
                    vin[deep-1] = bit[deep];
                    for (int i = deep-2; i >= 0; --i) vin[i] = vin[i+1] + 3 + bit[i+1];
            int LENGTH = vin[0] + 3 + bit[0];
                    space[0] = 0;
                    for (int i = 1; i <= deep; ++i) {
                            space[i] = space[i-1] + bit[i-1] + 3;
                    }
    
            //printf("LENGTH = %d\n", LENGTH);
                    for (int i = 0; i < deep; ++i) {
                            for (int j = 1; j < LENGTH; ++j) putchar(' ');
                            putchar('1');
                            putchar('\n');
                            for (int j = 0; j < space[i]; ++j) putchar(' ');
                            printf("%I64d + ", a[i]);
                            for (int j = 0; j < vin[i]; ++j) putchar('-');
                            putchar('\n');
                    }
                    for (int j = 0; j < LENGTH-bit[deep]; ++j) putchar(' ');
                    printf("%I64d\n", a[deep]);
        }
        return 0;
    }
    /*
    LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRLLLLLLLLLLLLLLLLLLLLLLLLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRLLLLLLLLLLLLLLLLLLLLLLLLLLLRLRLRLLRLRLRLRLRLRRRRRRRRRRR
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度