繁体   English   中英

如何去掉 output 末尾的空格?

[英]How do I get rid of a space at the end of output?

到目前为止,这是我生成斐波那契数的代码。 我一直得到一个零,因为它在最后添加了一个空格键,但我需要在数字之间留一个空格。

#include<stdio.h>
void fib(int);

int main()
{
    int n = 0;
    while (n <= 0 || n > 70)
    {
        printf("enter the value(between 0 and 70)");
        scanf("%d", &n);
        if (n <= 0 || n >= 70)
            printf("wrong input\n");
    }
    fib(n);
    return 0;
}

void fib(int n)
{
    /* Declare an array to store Fibonacci numbers. */
    int f[n]; // 1 extra to handle case, n = 0
    int i;

    /* 0th and 1st number of the series are 0 and 1*/
    f[0] = 0;
    f[1] = 1;
    printf("%d %d ", f[0], f[1]);
    for (i = 2; i < n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        printf("%d ", f[i]);
    }
}

删除第一个printf中的尾随空格,并在循环中的数字之前而不是之后打印空格。

printf("%d %d", f[0], f[1]);
for (i = 2; i < n; i++) {
    f[i] = f[i - 1] + f[i - 2];
    printf(" %d", f[i]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM