【问题标题】:How to print diagonals of a N X N box? Check my code?如何打印 N X N 框的对角线?检查我的代码?
【发布时间】:2020-09-19 04:41:28
【问题描述】:
int main()
{
    int n, i, j;

    cin >> n;

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i == 0 || i + j == n - 1)
            {
                cout << "*";
            }
            else
                cout << " ";
        }

        cout << "\n";
    }
    return 0;
}

预期的输出是这样的(如果我输入 no 5,它应该打印 5x5 的对角线)

  *     *
   *  *
    *
  *  *
*      *

【问题讨论】:

  • 代码应该缩进以使其可读,实际输出也应该包括在内,并且在提出问题的问题中应该有一些文本。

标签: c++ c c++14


【解决方案1】:

您不需要 2 个 for 循环来绘制图案。一个循环就可以了。

只需跟踪两颗星星的位置即可。

喜欢:

#include <stdio.h>

int main(void) {
    int n = 6;
    int starApos = 0;
    int starBpos = n - 1;
    for (int i=0; i < n; ++i)
    {
        if (starApos < starBpos)
        {
            printf("%*s", starApos, "");                // spaces before A
            printf("*");                                // A
            printf("%*s", starBpos - starApos - 1, ""); // spaces between A and B
            printf("*");                                // B
        }
        else if (starApos == starBpos)
        {
            printf("%*s", starApos, "");                // special case: only 1 *
            printf("*");
        }
        else
        {
            printf("%*s", starBpos, "");
            printf("*");
            printf("%*s", starApos - starBpos -1, "");
            printf("*");
        }
        printf("\n");

        ++starApos;    // Move positions
        --starBpos;
    }
    return 0;
}

输出 n = 6:

*    *
 *  *
  **
  **
 *  *
*    *

输出 n = 5:

*   *
 * *
  *
 * *
*   *

【讨论】:

    【解决方案2】:

    要正确地使拳头对角线,而不是 i + j == n - 1 放入 i + j == n + 1。 最后,加上|| j == i,形成另一条对角线。

    我还建议您删除 i == 0i 从 1 开始,并且只会上升,因此永远不会为 0),为您的代码添加更好的缩进并将 #include &lt;bits/stdc++.h&gt; 替换为 #include &lt;iostream&gt; 如果你真的不需要它。

    您的程序应如下所示:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++)
        {
    
            for(int j = 1; j <= n; j++)
            {
                if (i + j == n + 1 || j == i)
                    cout << "*";
                else
                    cout << " ";
            }
            cout << "\n";
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      对于一个完全用 C 语言编写的程序,下面的代码可以工作。确保在 X 中的每个 * 处查找行和列“索引”之间的关系(即假设您正在形成的 X 是一个二维数组)。

      #include <stdio.h>
      
      int main(void) {
          int size = 0;
      
          // Ask user to enter size of x, then scan input 
          printf ("Enter size: ");
          scanf("%d", &size);
          
          // Only an odd input can be accepted 
          if (size > 0 && size % 2 != 0) {
      
              for (int row = 0; row < size; row++) {
              
                  for (int col = 0; col < size; col++) {
                  
                      if (row == col) {
                          //Print one diagonal 
                          printf("*");
      
                      } else if (row + col == size - 1) {
                          //Print the other diagonal 
                          printf("*");
      
                      } else {
                          //Print empty spaces 
                          printf(" ");
                      }
                  } 
                  
                  //Move to next row 
                  printf ("\n");
              }
      
          }
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-14
        • 1970-01-01
        • 2017-03-06
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 2017-08-05
        • 2012-06-26
        • 1970-01-01
        相关资源
        最近更新 更多