下面是用C语言编写的解决上述问题的程序:

#include <stdio.h>

void print_pyramid(char x, int a, int h) {
    // 初始化字母计数
    int count_z = 0;
    
    // 打印金字塔
    for (int i = 0; i < h; i++) {
        // 打印每行的空格
        for (int j = 0; j < h - i - 1; j++) {
            printf(" ");
        }
        
        // 打印每行的字母
        for (int j = 0; j < a + 2 * i; j++) {
            // 判断是否为首字母,如果是则打印x
            if (j == 0) {
                printf('%c', x);
            }
            // 判断是否为顶部或底部的字母,如果是则打印x
            else if (i == h - 1 || j == a + 2 * i - 1) {
                printf('%c', x);
            }
            // 否则打印递减的字母
            else {
                char letter = x - (j % 26);
                if (letter < 'a') {
                    letter += 26;
                }
                printf('%c', letter);
                // 统计字母砖块数量
                if (letter == 'Z' || letter == 'z') {
                    count_z++;
                }
            }
        }
        printf("\n");
    }
    
    // 打印结果
    printf("所建金字塔共使用%d个字母砖块Z\n", count_z);
}

int main() {
    char x;
    int a, h;
    
    printf("请输入 x a h (例如:D 1 3): ");
    scanf("%c %d %d", &x, &a, &h);
    
    print_pyramid(x, a, h);
    
    return 0;
}

你可以将以上代码保存为一个.c文件,然后使用C编译器编译并运行该程序。程序会要求你输入金字塔的参数(x, a, h),然后输出金字塔的图像,并显示金字塔共使用多少个字母Z或z。

C语言金字塔图案生成器 - 带字母统计功能

原文地址: https://www.cveoy.top/t/topic/yzL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录