是否可以使用循环输出小键盘的模式?

Is it possible to output the numpad's pattern using loops?

我几个月前才开始学习 C(一般编码)。今天早些时候,当我在 class 时,我看着小键盘,想知道我是否能够使用 C 中的嵌套循环来复制该模式。

7 8 9
4 5 6
1 2 3 // This pattern.

我试着自己做了一点,主要使用 for 循环。感谢您的帮助。

#include<stdio.h>

int main()
{
    int row, col, i;

printf("Up to what integer? ");
scanf("%d", &row);

for(i=1; i<=row; i++)
{
    for(col=1; col<=10; col++)
    {
        printf(" %d ", i*col);
    }
    printf("\n");
    }
}

编辑:添加补充代码。像这样,除了打印 3 行和 3 列。

你可以这样做:

for(int i = 0; i < 3; ++i){
  for(int j = 3; j > 0; --j)
    printf("%d ", (10 - j) - i * 3);

  printf("\n");
}

小键盘模式有等式 3*i + ji20j13.

所以使用这些值作为嵌套for循环中ij的上限和下限。

#include <stdio.h>

int main(){

    for(int i = 2; i >= 0; i--){
        for(int j = 1; j <= 3; j++)
            printf("%d ", 3 * i + j);
        printf("\n");
    }
  return 0;
}

现场观看here