Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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_Design Patterns_Pattern Matching - Fatal编程技术网

C中的图案打印,用于以垂直图案打印数字

C中的图案打印,用于以垂直图案打印数字,c,design-patterns,pattern-matching,C,Design Patterns,Pattern Matching,我写了一个函数来打印下面的模式。 例如,如果n值为4,则模式为 1 2 7 3 6 8 4 5 9 10 1 2 9 3 8 10 4 7 11 14 5 6 12 13 15 或者,如果n的值为5,则模式为 1 2 7 3 6 8 4 5 9 10 1 2 9 3 8 10 4 7 11 14 5 6 12 13 15 我的函数给出了前两个块,但没有给出下一个块。我被困在这里很久了 我的职能是 int printPattern(int n) { int row, column,

我写了一个函数来打印下面的模式。 例如,如果n值为4,则模式为

1
2 7
3 6 8
4 5 9 10
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
或者,如果n的值为5,则模式为

1
2 7
3 6 8
4 5 9 10
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
我的函数给出了前两个块,但没有给出下一个块。我被困在这里很久了

我的职能是

int printPattern(int n) {
    int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
    for(row = 1; row <= n; row++) {
        fwdCtr = row;
        for(column = 1; column <= row; column++) {
            if(column % 2 != 0) {
                printf("%d ", fwdCtr++);
            } else {
                evenCtr = fwdCtr + ctr;
                printf("%d ", evenCtr);
                ctr = ctr - 2;
            }
        }
        printf("\n");
    }     
}

请给出修改建议

以下代码应该可以做到这一点:

#include <stdio.h>

void f(int n)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j=0; j<=i; ++j)
        {
            // Calculate the numbers used so far by previous columns
            int x = 0;
            for(int v=0; v<j;++v)
            {
                x = x + (n-v);  
            }

            if ((j % 2) == 0)
            {
                // even columns
                printf("%d ", x+i-j+1);
            }
            else
            {
                // odd columns
                printf("%d ", x+n-i);
            }
        }
        printf("\n");
    }
}

int main(void) 
{
    f(5);
    return 0;
}

简单的方法是根据行和列以及
n
的值打印正确的数字,如下所示

int main(void)
{
    int n = 20;
    for (int row = 0; row < n; row++) {
        for (int col = 0; col <= row; col++)
           printf("%3d ", 1 + col*n - (col-1)*col/2 + (col%2 ? n-1-row : row-col));
        printf("\n");
    }
}
int main(无效)
{
int n=20;
对于(int行=0;行对于(int col=0;col),我将在2d数组中准备值,然后打印数组左下角的三角形。也许有一种更聪明的方法。所以问题是第三个和第四个col不起作用。但它们完全按照您的要求执行。
printf(“%d”,fwdcr++);
在每一个奇数列上执行,结果是打印该列,然后递增。此外,
evenCtr=fwdtr+ctr;
在每一个偶数行执行,执行之间
ctr
递减两次,
fwdtr
递增一次(导致净递减1)。这正是您所注意到的。如果它不是您想要的,那么这可能是重写代码的时候,以使其足够清晰,不会引起您的混淆。我建议首先构建模型(可能使用上面建议的2D数组),然后再打印该模型的内容。在没有模型的情况下工作,并试图纯粹根据输入打印值,这会让您感到困惑。您能帮我编写代码吗?我甚至尝试了2D结构,但未能获得结果谢谢分享:)这很有帮助,但我想说,如果可能的话,请添加一些解释(可能是一行)。我看到了一行很好……唯一的一点是,对于更高的值,它可能会溢出。不是吗?嗯,是的……但这也是一种情况……只是说说而已。