我正在尝试制作一个程序,从用户处获取输入行数并打印这种类型的模式,例如如果“行数:5”,那么它应该打印此模式
我写了这段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;
}