C语言中规中矩的大树

c

没事干,写个树玩玩,真正的“撸树”

在Ubuntu终端、VScode终端显示

代码

#include "stdio.h"

int main(){
    int high = 5;           //层高
    int count = 5;          //层数
    int start;              //每层开始*数
    int width = 1 + 4*high; //末尾层最大宽度
    int flag_count = 0;     //*计数
    int root_count = 0,root_temp;     //树桩的大小

    /* 计算末尾宽度 */
    for(int i=0; i<count; i++){
        start = high / 2 ;
        width += start + (high);
        if(i == 0)
            root_count = width - (count+2)*2;
    }
    width = (width+2) / 2;
    printf("%d\n",width);

    /* 输出树 */
    start = 1;  //第一层的第一个为1
    for(int a=0; a<count; a++){
        flag_count = start;
        for(int b=0; b<high; b++){
            /* 打印空格 */
            for(int c=0; c<width-flag_count-b; c++)
                printf(" ");
            /* 打印* */
            for(int c=0; c<(flag_count+b)*2; c++)
                printf("*");
            flag_count += 2;
            printf("\n");
        }
        start += high / 2 + 1;
    }

    /* 打印树桩 */

    root_temp = (root_count+1)/2;
    for(int a=0; a<count+1; a++){
        for(int b=0; b<width-root_temp; b++)
            printf(" ");
        for(int c=0; c<root_count; c++)
            printf("*");
        printf("\n");
    }
}

相关文章

【数据结构】——哈夫曼树

速通回忆 这里是完整的代码和运行结果,可以直接选择看下面代码的思路去快速回忆哈夫曼树,或从下面理论部分开始学习 ``` #include "stdio.h" #include "stdlib.h" /* 哈夫曼树结点的结构体 */ typedef struct { int weight; /* 权重 */ int parent; /* 父母结点下标 */ int l...

c

强化C【C语言笔记】——位运算

位运算符C语言提供了六种位运算符 - &:按位与 - !:按位或 - ^:异或 - ~:取反 - <<:左移 - \>>:右移 按位与运算 **其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1,否则为0** 按位与运算通常用来对某些位清0 按位或运算 **其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就...

c

细说C语言【内存存储】

**本文章分为:内存结构、大小端存储、不同数据类型的存储方式** 内存结构 C语言中,对内存进行了划分。总共分为:栈区、堆区、代码区、常量区、全局数据区。 其中全局数据又可细分为:初始化静态数据区和未初始化静态数据区 栈区 - **存放函数执行时的局部变量、函数参数和函数返回值** - 栈区的**大小由操作系统决定** - 函数之间的调用是通过栈实现的,**调用函数就入栈,函...

c