没事干,写个树玩玩,真正的“撸树”
在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");
}
}