字母金字塔

问题描述 投票:0回答:1

我正在尝试制作一个程序,从用户处获取输入行数并打印这种类型的模式,例如如果“行数:5”,那么它应该打印此模式 OUTPUT I WANT

我写了这段C代码,

#include <stdio.h>

int main() {
    int rows, i, j, k;

    // Take input for the number of rows
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        k = 'A';
        for (j = 1; j <= 2 * rows - 1; j++) {
            // Adjusted condition to reduce one space in all rows
            if ((j <= rows - i || j >= rows + i) && !(i == 1 && j == rows)) {
                printf("%c", k);
                j < rows ? k++ : k--;
            } else {
                if (j != rows) {
                    printf(" ");
                }
                if (j == rows) {
                    k--;
                }
            }
        }
        printf("\n");
    }

    return 0;
}

执行上述代码后,我得到了这种类型的答案 UNDESIRED RESULT

c pyramid triangle
1个回答
0
投票

i got like that pic but it's not a pyrmid.Do you want a pyramid shaped like a space?

© www.soinside.com 2019 - 2024. All rights reserved.