解决 - C程序设计锯齿形初步实践 [英] Solve - C Programming Zig-Zag Pratice

查看:146
本文介绍了解决 - C程序设计锯齿形初步实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C的利用的练习的if-else 的printf 在控制台打印锯齿是这样的:

请参见图片:

在这里输入的形象描述

我已经尝试了很多方法,但未能保持始终。我真的需要帮助。谢谢你。

我已经尝试了code C语言编程,并打印一个三角形,然后尝试编辑这一项,但不能做任何更多的让我的锯齿形。

我解决我的问题了。谢谢你们这么多。

 的for(int i = 0;我小于5;我++){
    对于(INT J = 1; J< = 21; J ++){
        如果(J< = 9){
            如果(J - 我== || 5 J +我== 5){
                的printf(*);
            }其他{
                的printf();
            }
        }其他{
            如果(J +我= = || 13的J - 我== || 13 J +我== 21){
                的printf(*);
            }
            其他{
                的printf();
            }
        }
    }
    的printf(\\ n);
}


解决方案

确定,根据您的更新问题的尝试2。让我们先从基本结构。设置宽度和高度一些consts - 我们将把这些在多个地方,所以consts做出漂亮的简单的更改 - 并创建基本逻辑循环开始,以获得您想要的显示

  const int的其行= 5;
const int的数numCols = 21;
对于(INT行= 0;&行LT;其行++行)
{
    对于(INT COL = 0;&山坳下,数numCols ++ COL)
    {
        如果(COL ==行)
        {
            的printf(X);
        }
        其他
        {
            的printf();
        }
    }
    的printf(\\ n);
}

这给了我们:

  X
 X
  X
   X
    X

这是了错误的方式,并只给我们一冲程,但它是一个良好的开端。让我们用模函数,使花纹。重复发生在第9个字符,这意味着我们需要使用((其行 - 1)* 2)。我们的模数

  const int的modulusVal =((其行 -  1)* 2);
对于(INT行= 0;&行LT;其行++行)
{
    对于(INT COL = 0;&山坳下,数numCols ++ COL)
    {
        INT modCol =(COL%modulusVal);
        如果(modCol ==行)
        {
            的printf(X);
        }
        其他
        {
            的printf();
        }
    }
    的printf(\\ n);
}

所以,现在我们得到:

  X X X
 X X X
  X X X
   X X X
    X X X

这开始变得更加接近我们想要的东西很多。因此,到的上行。当modCol是行的范围内,这是0-4的downstrokes被显示。当modCol是过去的这个范围内,我们把它返回到范围从中减去的行数。

然后,我们通过从该行可以是最高值,这是其行减去它颠倒,它 - 1。这意味着,当modCol是0将成为4,当它为1时它会变得3中,当它在二将保持不变。

 为(INT行= 0;&行LT;其行++行)
{
    对于(INT COL = 0;&山坳下,数numCols ++ COL)
    {
        INT modCol =(COL%modulusVal);
        如果(modCol> =其行)
        {
            modCol - =其行;
            modCol =((numRows行 - 1) - modCol);
        }        如果(modCol ==行)
        {
            的printf(X);
        }
        其他
        {
            的printf();
        }
    }
    的printf(\\ n);
}

现在我们得到:

  X X X
 X X X
  X X X X X
   X X X X X
    XX XX点¯x

关闭,但没有雪茄。第5栏被视为0列,但我们需要它被视为第1列进行此更改修复它:

  modCol =((其行 -  1) - (modCol + 1));输出:
X X X
 X X X X X
  X X X X X
   X X X X X
    X X X

这有我们想要的模式,但在左上角开始,当我们想的左下角。简单的办法。反转modCol它在我们反其道而行,当它大于/小于/到其行平等的,我们在计算之后。

 为(INT行= 0;&行LT;其行++行)
{
    对于(INT COL = 0;&山坳下,数numCols ++ COL)
    {
        INT modCol =(COL%modulusVal);
        如果(modCol> =其行)
        {
            modCol - =其行;
            modCol =((numRows行 - 1) - (modCol + 1));
        }
        modCol =((numRows行 - 1) - modCol);        如果(modCol ==行)
        {
            的printf(X);
        }
        其他
        {
            的printf();
        }
    }
    的printf(\\ n);
}

最后,我们得到我们想要的输出:

  X X X
   X X X X X
  X X X X X
 X X X X X
X X X

我希望这有助于和合理易于理解。

I have an exercise in Objective-C making use of for, if-else and printf to print a Zig-Zag like this in the console:

Please see image:

I have tried many ways but keep failing all times. I really need help. Thank you.

I have tried a code with C programming and print a triangle then try to edit this one, but can't do anything more to get my Zig-Zag.

I have solve my problems already. Thank you guys so much.

 for (int i = 0; i < 5; i++) {
    for(int j = 1; j<= 21; j++ ){
        if(j<=9){
            if(j - i == 5 || j+ i == 5){
                printf("*") ;
            }else{
                printf(" ");
            }
        }else{
            if(j+i == 13 || j - i == 13 || j + i == 21){
                printf("*") ;
            }
            else{
                printf(" ");
            }
        }
    }
    printf("\n");
}

解决方案

OK, attempt 2 based on your updated question. Let's start with the basic structure. Set some consts for the width and height - we will refer to these in multiple places, so consts make for nice easy changes - and create a loop with basic logic to start to get the display you want.

const int numRows = 5;
const int numCols = 21;
for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        if (col == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

This gives us:

X                    
 X                   
  X                  
   X                 
    X                

which is the wrong way up and only gives us one "downstroke", but it is a good start. Let's use the modulus function to make the pattern repeat. The repeat happens on the 9th character, which suggests we need to use ((numRows - 1) * 2) for our modulus.

const int modulusVal = ((numRows - 1) * 2);
for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

So now we get:

X       X       X    
 X       X       X   
  X       X       X  
   X       X       X 
    X       X       X

which is starting to get a lot closer to what we want. So, on to the upstrokes. The downstrokes are displayed when modCol is in the range of row, which is 0-4. When modCol is past this range, we bring it back into range by subtracting the number of rows from it.

Then we "invert" it by subtracting it from the highest value that row can be, which is numRows - 1. This means that when modCol is 0 it will become 4, when it is 1 it will become 3, when it is 2 it will be unchanged.

for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol >= numRows)
        {
            modCol -= numRows;
            modCol = ((numRows - 1) - modCol);
        }

        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

Now we get:

X       X       X    
 X       X       X   
  X    X  X    X  X  
   X  X    X  X    X 
    XX      XX      X

Close, but no cigar. Column 5 is being treated as column 0, but we need it to be treated as column 1. Make this change to fix it:

modCol = ((numRows - 1) - (modCol + 1));

Output:
X       X       X    
 X     X X     X X   
  X   X   X   X   X  
   X X     X X     X 
    X       X       X

This has the pattern we want, but starts at the top left when we want the bottom left. Easy fix. Invert modCol after it has been calculated in the same we we invert it when it is greater/equal than/to numRows.

for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol >= numRows)
        {
            modCol -= numRows;
            modCol = ((numRows - 1) - (modCol + 1));
        }
        modCol = ((numRows - 1) - modCol);

        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

Finally we get the output we want:

    X       X       X
   X X     X X     X 
  X   X   X   X   X  
 X     X X     X X   
X       X       X    

I hope this helps and is reasonably easy to understand.

这篇关于解决 - C程序设计锯齿形初步实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆