C学习之路-使用C打印金字塔

#include <stdio.h>

//for循环
func1(int height){
   int width=1;
   int height_temp=1;
  for(;height_temp<=height;height_temp++){
    for(width;width<=(height-height_temp);width++){
      printf(" ");
     }
     width=1;
    for(width;width<=(height_temp*2-1);width++){
     printf("*");
     }
     width=1;
    printf("\n");
  
  }
}
//while循环
func2(int height){
  int width=1;
  int height_temp=1;
  while(height_temp<=height){
    while(width<=(height-height_temp)){
      printf(" ");
      width++;
     };
     width=1;
    while(width<=(height_temp*2-1)){
      printf("*");
      width++;
     };
     width=1;
     printf("\n");
     height_temp++;
  }
}

//do_while循环
func3(int height){
  int height_temp=1;
  int sum = height;
  do{
    int space =  (sum-height_temp);
    do{
	  if(space<=0)break;
      printf(" ");
    }while(space-=1);

     int star = 0;
     star=(height_temp*2)-1;

	 do{
	  if(star<=0)break;
	  printf("*");
	 }while(star-=1);

	 height_temp++;
    printf("\n");
  }while(height-=1);
}

//打印横向*
func5_1(int width){
  if(width<=0)return;
  printf("*");
  func5_1(width-=1);
}
//打印空格
func5_2(int space){
  if(space<=0)return;
  printf(" ");
  func5_2(space-=1);
}

//递归
func5(int height,int sum){
  if(height<=0)return;
  func5(height-=1,sum);
  int star = height+1;
  func5_2(sum-star);
  func5_1(star*2-1);
  printf("\n");
}




int main(){

    printf("请输入你需要建造的金字塔层数:");
    int height;
    scanf("%d",&height);
	func1(height);
	func2(height);
    func3(height);
    func5(height,height);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值