首页 > 解决方案 > 是否可以使用循环输出数字键盘的模式?

问题描述

几个月前我刚开始学习 C(一般编码)。今天早些时候,当我在课堂上时,我看着小键盘,想知道我是否能够使用 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 列。

标签: cdesign-patternsprintingnested-loops

解决方案


以下是您的操作方法:

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

  printf("\n");
}

推荐阅读