如何在C中打印如下图案?

问题描述 投票:0回答:1
#1234567
1#345678
23#56789
345#7890
4567#901
56789#12
678901#3
7890123#

这是我的代码

int main() {
    int pattern;
    int rows, columns;

    printf("Enter the pattern: ");
    scanf("%d", & pattern);

    for (rows = 1; rows <= pattern; rows++) {
        for (columns = 1; columns <= pattern; columns++) {
            if (rows == columns)
                printf("#");
            else
                printf("%d", columns);
        }
        printf("\n");
    }

    return 0;
}
c
1个回答
1
投票

从0到模式1启动内循环和外循环。打印(columns+rows)%10。你的工作将完成。请参阅下面的代码更改:

int main() {
    int pattern;
    int rows, columns;

    printf("Enter the pattern: ");
    scanf("%d", & pattern);

    for (rows = 0; rows < pattern; rows++) {
        for (columns = 0; columns < pattern; columns++) {
            if (rows == columns)
                printf("#");
            else
                printf("%d", (columns+rows)%10);
        }
        printf("\n");
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.