【问题标题】:How to Print an n-by-n Matrix with r spaces between its columns?如何打印列之间有 r 个空格的 n×n 矩阵?
【发布时间】:2021-12-13 22:09:30
【问题描述】:

打印一个具有 n 维的矩阵,其列之间的距离为 r。对于 n = 4 和 r = 7,这是如下所示的输出,其中每列之间有 7 个空格。 (不允许使用矩阵或数组)

这里的主要问题是我试图弄清楚如何根据nri 概括 1 的位置。我被卡住了,因为似乎没有简单的方法可以做到这一点,或者我只是想念它。

这是解决这类问题的好方法还是有更好的方法?

#include <stdio.h>

int main() {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance


  int i, j, k;
  for (i = 0; i < n; i++) { // Row
    for (j = 0; j < (r + 1) * n; j++) {
      if (j % r == 0) {
        if (j == r * (1 + i) && j != 0) { // For 1s
          printf("1");
        } else {
          printf("0"); // For -0s
        }
      } else {
        printf("X");
      }
    }
    printf("\n");
  }
  return 0;
}

【问题讨论】:

  • 你试过用printf(" ");替换printf("X");吗?此外,如果您只想为前导对角线 (i == j) 打印 1 并为所有其他元素打印 0,那么您的循环逻辑似乎非常复杂。
  • printf("%10s", "X") 在 10 个字符的列中打印 X。

标签: c for-loop printf nested-loops


【解决方案1】:

图片不是很好。我假设第一列是左对齐的,并且距离包括数字。

#include <stdio.h>

void printMatrix(int n, int d)
{
    for(int row = 0; row < n; row++)
    {
        for(int col = 0; col < n; col++)
            printf("%*d", !!col * d + 1, row == col);
        printf("\n");
    }
}


int main(void) {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance
  printf("\n");

  printMatrix(n,r);
}

https://godbolt.org/z/KTcvTre1E

如果你不允许使用那些“花哨”的格式:

#include <stdio.h>

void printMatrix(int n, int d)
{
    for(int row = 0; row < n; row++)
    {
        for(int col = 0; col < n; col++)
        {
            printf("%d", row == col);
            for(int space = 0; col != n && space < d; space++)
                printf(" ");
        }
        printf("\n");
    }
}

int main(void) {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance
  printf("\n");

  printMatrix(n,r);
}

https://godbolt.org/z/Y5K8h74e5

【讨论】:

  • 字段宽度应为d+1,或调用printMatrix,第二个参数为r+1
  • ...但我喜欢双刘海,所以!!col * d + 1(假设在第一列之前没有空格)。
  • @AdrianMole 从这张照片很难猜到。我认为第一列应该左对齐,距离包括数字
  • 嗯。 OP 说 每列之间有 7 个空格
  • @nocomment 尝试自己解决。很简单。 8 个字符被删除
【解决方案2】:

注意printf 格式说明符(如用于十进制整数输出的%i)可以采用可选的字段宽度 参数;因此,printf("%8i", x);,当x 的值为1 时,将打印 1(前七个空格,使总字段宽度为8)。此外,如果您将* 指定为字段宽度,则该宽度将被指定为printf 调用的额外参数(在您的情况下为r + 1),紧接在要打印的参数之前。

来自cppreference

  • (可选)整数值或*,指定最小字段宽度。结果用 space 字符填充(默认情况下),如果 需要,右对齐时在左侧,如果右对齐,则在右侧 左对齐。在使用 * 的情况下,宽度由下式指定 int 类型的附加参数,出现在 要转换的参数和提供精度的参数,如果一个 提供。如果参数的值为负,则结果为 - 标志指定和正字段宽度。 (注:这是 最小宽度:该值永远不会被截断。)

因此,对于您的问题,假设 r 在第一列之前以及列之间有空格,并假设您希望每个前导对角线元素(当行 = 列时)使用 1 并为所有其他元素使用 0,以下,大大简化的代码可以解决问题:

#include <stdio.h>

int main(void)
{
    int n; // Dimensions
    int r; // Distance between columns
    printf("Enter n: ");
    scanf("%i", &n); // Dimensions
    printf("Enter the distance between columns in matrix: ");
    scanf("%i", &r); // Distance
    for (int i = 0; i < n; i++) { // Row
        for (int j = 0; j < n; j++) { // Column
            int x = (j == i) ? 1 : 0;
            printf("%*i", r + 1, x); // Field width is #spaces PLUS 1 for the digit itself
        }
        printf("\n");
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多