在表上,n值应与值fibo的行配合空格。

printf("n | ");

for (i = 1; i < = n ; i++)
{
    printf("%d", i);

    for (wert=0; wert < = (hochn(fibo(i)) - hochn(i)) ; wert++)
    {
        printf(" ");
    }
}


其他函数“ hochn”找到10的幂的值。

int hochn (int b)
{
    int tmp;

    for(tmp =0; b > 10 ; tmp++)
    {
        b = (b / 10);
    }

    return tmp;
}


另外,“ fibo”查找斐波那契数的值。

该程序的实际运行如下所示:

n | 1 2 3 4 5 6 7  8  9  10  11 12  13  14
--------------------------------------------
f | 1 1 2 3 5 8 13 21 34 55 89 144 233 377


为什么n = 11只有1个空格,而在n = 12时又返回2个空格?

最佳答案

问题在于,对hochn()函数返回的值进行简单测试后,会得出错误的答案。您的版本在以下代码中为hochn0();固定版本为hochn1()。我也包括了布局的测试代码。请注意,您在打印标题行时会在数字之后而不是之前放置空格,这也会使事情复杂化,并在行尾留下尾随空格。您可能还注意到,您的问题始于7而不是11。

在下面的代码中,我使函数静态化,因为否则使用的编译器选项要求在定义函数之前先声明函数。

#include <stdio.h>

static int hochn0(int b)
{
    int tmp;

    for (tmp = 0; b > 10; tmp++)
        b = (b / 10);

    return tmp;
}

/* hochn1() - return int(log10(b)) - 1, and 0 for b <= 0 */
static int hochn1(int b)
{
    int tmp;

    for (tmp = 0; (b /= 10) > 0; tmp++)
        ;

    return tmp;
}

static int fibo(int n)
{
    int f0 = 0;
    int f1 = 1;
    for (int i = 0; i < n - 1; i++)
    {
        int fn = f0 + f1;
        f0 = f1;
        f1 = fn;
    }
    return f1;
}

int main(void)
{
    for (int i = 0; i < 120; i++)
    {
        int h0 = hochn0(i);
        int h1 = hochn1(i);
        if (h0 != h1)
            printf("%3d: hochn0 = %d, hochn1 = %d\n", i, hochn0(i), hochn1(i));
    }

    int n = 14;

    /* Broken - print spaces after number */
    printf("n | ");
    for (int i = 1; i <= n; i++)
    {
        printf("%d", i);
        for (int wert = 0; wert <= (hochn0(fibo(i)) - hochn0(i)); wert++)
            putchar(' ');
    }
    putchar('\n');

    /* Fixed - print spaces before number */
    printf("n |");
    for (int i = 1; i <= n; i++)
    {
        for (int wert = 0; wert <= (hochn1(fibo(i)) - hochn1(i)); wert++)
            putchar(' ');
        printf("%d", i);
    }
    putchar('\n');

    /* Succinct fixed */
    int count = 0;
    count += printf("n |");
    for (int i = 1; i <= n; i++)
        count += printf("%*s%d", (hochn1(fibo(i)) - hochn1(i)) + 1, " ", i);
    putchar('\n');

    for (int i = 0; i < count; i++)
        putchar('-');
    putchar('\n');

    /* Result line */
    printf("f |");
    for (int i = 1; i <= n; i++)
        printf(" %d", fibo(i));
    putchar('\n');

    return 0;
}


输出示例:

第一部分标识hochn0()hochn1()产生不同答案的数字。最后几行显示输出的显示方式,包括原始版本和几个修订版本。

 10: hochn0 = 0, hochn1 = 1
100: hochn0 = 1, hochn1 = 2
101: hochn0 = 1, hochn1 = 2
102: hochn0 = 1, hochn1 = 2
103: hochn0 = 1, hochn1 = 2
104: hochn0 = 1, hochn1 = 2
105: hochn0 = 1, hochn1 = 2
106: hochn0 = 1, hochn1 = 2
107: hochn0 = 1, hochn1 = 2
108: hochn0 = 1, hochn1 = 2
109: hochn0 = 1, hochn1 = 2
n | 1 2 3 4 5 6 7  8  9  10  11 12  13  14
n | 1 2 3 4 5 6  7  8  9 10 11  12  13  14
n | 1 2 3 4 5 6  7  8  9 10 11  12  13  14
------------------------------------------
f | 1 1 2 3 5 8 13 21 34 55 89 144 233 377

关于c - 对应于C中斐波那契数的长度的斐波那契间隔图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27629773/

10-12 22:10