Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的数字金字塔_C_Loops_For Loop - Fatal编程技术网

C中的数字金字塔

C中的数字金字塔,c,loops,for-loop,C,Loops,For Loop,练习:制作一个程序,读取自然数n小于100,并绘制示例输出格式的棱锥体,每个棱锥体输出n=11的n行: 01 02 02 03 03 03 04 04 04 04 05 05 05 05 05 06 06 06 06 06 06 07 07 07 07 07 07 07 08 08 08 08 08 08 08 08 09 09 09 09 09 09 09 09 09 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11

练习:制作一个程序,读取自然数n小于100,并绘制示例输出格式的棱锥体,每个棱锥体输出n=11的n行:

01
02 02
03 03 03
04 04 04 04
05 05 05 05 05
06 06 06 06 06 06 
07 07 07 07 07 07 07
08 08 08 08 08 08 08 08 
09 09 09 09 09 09 09 09 09 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11 
01
01 02
01 02 03
01 02 03 04
01 02 03 04 05
01 02 03 04 05 06
01 02 03 04 05 06 07
01 02 03 04 05 06 07 08
01 02 03 04 05 06 07 08 09
01 02 03 04 05 06 07 08 09 10
01 02 03 04 05 06 07 08 09 10 11
代码:

#包括
int main()
{
inti,j,n;
scanf(“%d”和“&n”);
对于(i=0;i
我的代码接收到来自在线评委的“表示错误”,可能是因为每行的最后一个数字后面都有空格

有关于如何修复它的提示吗


我知道我已经发布了一个类似的问题,但我不认为我完全理解了解决方案。

与其在每次迭代中使用空格作为后缀,不如在第一次迭代中使用空格作为前缀

与此相反:

    for(j = 0; j <= i; j++)
    {
        if(i + 1 < 10)
            printf("0%d ", i+1);
        else if(i + 1 >= 10)
            printf("%d ", i+1);
    }
(j=0;j=10)的

printf(“%d”,i+1);
}
使用前导空格进行细微更改:

    for(j = 0; j <= i; j++)
    {
        if (j != 0)
            printf(" "); // print the leading space

        if(i + 1 < 10)
            printf("0%d", i+1);
        else if(i + 1 >= 10)
            printf("%d", i+1);
    }
(j=0;j=10)的

printf(“%d”,i+1);
}
如果你想看起来聪明点:

    for(j = 0; j <= i; j++)
    {
        int val = i+1;
        const char* prefix1 = (j==0) ? "" : " ";
        printf("%s%02d", prefix1, val);
    }

用于(j=0;j打印两个棱锥体的更短版本:

#include <stdio.h>

void pyramid1(int n)
{
    for(int i=1; i<=n; ++i) {
        for(int j=0; j<i; ++j)
            printf("%s%02d", j? " " : "", i);
        printf("\n");
    }
}

void pyramid2(int n)
{
    for(int i=1; i<=n; ++i) {
        for(int j=1; j<=i; ++j)
            printf("%s%02d", j>1? " " : "", j);
        printf("\n");
    }
}


int main(void) {
    int n;
    scanf("%d",&n);
    pyramid1(n);
    printf("\n\n");
    pyramid2(n);

    return 0;
}

避免尾随空白的一种方法是:

for (int i = 0; i < n; i++)
{
    const char *pad = "";
    for (int j = 0; j <= i; j++)
    {
        printf("%s%.2d", pad, i+1);
        pad = " ";
    }
    putchar('\n');
}

(我没有检查循环限制(
I
j)这是两种金字塔的解决方案

void pyr(int n, int type)
{
    for(int row = 1; row <= n; row++)
        for(int col = 1; col <= row; col++)
            printf("%02d%c", type ? col : row , col == row ? '\n' : ' ');
}


int main()
{
    pyr(11,0);
    pyr(11,1);
}
void pyr(整数n,整数类型)
{

对于(int row=1;row好的,你真正理解代码是很重要的。你能确定这个额外的空间可能来自哪里吗?我同意Steve的说法。根据SO的家庭作业政策,@mamounothman这不会做你认为它会做的事。仅供参考,这里还有其他问题,OP不清楚代码是从哪里来的,所以是不是真的对他们帮助不大。这基本上是同一个基本问题,所以我希望OP能很快学会这项技术。我投票的范围太广了,因为我在这里看不到真正的努力。if/else测试可以结合到
printf(“%02d”,I+1);
printf(%.2d,I+1);
Data: 01, 02, 03
void pyr(int n, int type)
{
    for(int row = 1; row <= n; row++)
        for(int col = 1; col <= row; col++)
            printf("%02d%c", type ? col : row , col == row ? '\n' : ' ');
}


int main()
{
    pyr(11,0);
    pyr(11,1);
}