c++ 输出图形

// output_new.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <math.h>
/*
*  输出图案
*/

//限制条件:只能用两句输出(一共两句)
// cout<<"#" cout<<"\n"

//思路,一共有多少行,每一行要输出多少个,行与列的关系式

/*
* 1.半个倒三角
* 问题分解:
*/


//1.1 输出一行
void print_oneline(int n){
 for(int i=0;i<n;i++){cout<<"#";}
}
//1.2 输出方形
void print_square(int n){
 for(int row=0;row<n;row++){
  for(int column=0;column<n;column++){
   cout<<"#";
  }
  cout<<"\n";
 }
}
//1.3 输出半方形
/*
* ####
* ###
* ##
* #
*/
//行数0-n    r
//列数n-0    c=n-r
void  half_triangle(int n){
 int  R=n;//行数 //注意编号,这里我们从第一行开始(适合人类)
 int  C=0;
 for(int row=1;row<=R;row++){ //行数0-n
  C=R-(row-1); //每一行有多少个输出位
  for(int column=1;column<=C;column++){//列数n-0
   cout<<"#";
  }
  cout<<"\n";
 }
}

//2 侧三角
/*
* #
* ##
* ###
* ####
* ###
* ##
* #
*/

//2.1
//行数: 0---(2n-1)
//列数: 0---n--0
//  0 1 2 3 4 5 6
//  0 1 2 3 2 1 0
//===》转换,利用绝对值
// 1 2 3 4 5 6 7
// 1 2 3 4 3 2 1
//===>
// 1 2 3 4  5  6  7    r
// 3 2 1 0 -1 -2 -3   4-r
// 3 2 1 0  1  2  3   abs(4-r)
// 1 2 3 4  3  2  1   4-abs(4-r)
void  double_half_triangle(int n){
 int  R=n*2-1;//行数 //注意编号,这里我们从第一行开始(适合人类)
 int  C=0;
 for(int row=1;row<=R;row++){
  C=n-abs(n-row); //每一行有多少个输出位,(这里C与n=R/2+1有关)
  for(int column=1;column<=C;column++){
   cout<<"#";
  }
  cout<<"\n";
 }
}

//3 倒三角变
/*
*   ######
*    ####
*     ##
*/
//3.1 分解:半三角变种
//r: 1 2 3   r
//c: 6 3 2   c=2*(n-r)
//每行起始位置:1 2 3
void  anti_half_triangle(int n){
 int  R=n;//行数 //注意编号,这里我们从第一行开始(适合人类)
 int  C=0;
 for(int row=1;row<=n;row++){
  for(int i=0;i<row-1;i++){  //空格是0,1,2,3...
   cout<<' '; 
  }
  C=2*(n-(row-1)); //每一行有多少个输出位
  for(int column=1;column<=C;column++){
   cout<<"#";
  }
  cout<<"\n";
 }
}

//3 侧三角变种
/*
*     ##
*    ####
*   ######
*   ######
*    ####
*     ##
*/
//行数: 1 - 2n
//列数: 2---n--n--2
//===》转换,利用绝对值
// 1 2 3 4 5
// 2 4 6 4 2
//===>
// 1 2 3  4  5      r
// 4 2 0 -2 -4   2*(3-r)
// 4 2 0  2  4   2*abs(3-r)
// 2 4 6  4  2   2*3-2abs(3-r)
// =======>注意输出偶数的绝对值转换方式是+0.5
void  double_double_half_triangle(int n){
 int  R=n*2;//行数 //注意编号,这里我们从第一行开始(适合人类)
 int  C0=0;//前缀空格
 int  C=0;
 for(int row=1;row<=R;row++){
  C0=(abs(n+0.5-row)+0.5);
  for(int i=0;i<C0;i++){
   cout<<' ';
  }
  C=2*(abs(n-(abs(n+0.5-row)+0.5))+1);
  for(int column=0;column<C;column++){
   cout<<"#";
  }
  cout<<"\n";
 }
}

//4 扩展
/*
* #            #
*  ##        ##
*   ###    ###
*    ########
*    ########
*   ###    ###
*  ##        ##
* #            #
*
*/


//4.2 再分解
/*
* #          
*  ##       
*   ###   
*    ####
*/
void f4_2(int n){
 int  R=n;//行数
 int  C0=0;//前缀空格
 int  C=0;//#个数
 for(int row=1;row<=n;row++){
  C0=row-1;
  for(int i=0;i<C0;i++){
   cout<<' ';
  }
  C=row;
  for(int column=0;column<C;column++){
   cout<<"#";
  }
  cout<<"\n";
 } 
}
//4.1分解
/*
* #            #
*  ##        ##
*   ###    ###
*    ########
*/
void f4_1(int n){
 int  R=n;//行数
 int  C0=0;//前缀空格
 int  C1=0;//中间空格
 int  C=0;//#个数(前后各一半)
 for(int row=1;row<=n;row++){
  //开始位置
  C0=row-1;
  for(int i=0;i<C0;i++){
   cout<<' ';
  }
  C=row;
  for(int column=0;column<C;column++){
   cout<<"#";
  }
  //后面开始位置
  C1=(n-row)*4;
  for(int j=0;j<C1;j++){
   cout<<' ';
  }
  for(int column1=0;column1<C;column1++){
   cout<<"#";
  }
  cout<<"\n";
 } 
}
//4 扩展
/*
* #            #
*  ##        ##
*   ###    ###
*    ########
*    ########
*   ###    ###
*  ##        ##
* #            #
*
*/
//单
void f4a(int n){
 int  R=n*2-1;//行数
 int  C0=0;//前缀空格
 int  C1=0;//中间空格
 int  C=0;//#个数(前后各一半)
 for(int row=1;row<=R;row++){
  //开始位置
  C0=n-abs(n-row)-1;
  for(int i=0;i<C0;i++){
   cout<<' ';
  }
  C=n-abs(n-row);
  for(int column=0;column<C;column++){
   cout<<"#";
  }
  //后面开始位置
  C1=abs(n-row)*4;
  for(int j=0;j<C1;j++){
   cout<<' ';
  }
  for(int column1=0;column1<C;column1++){
   cout<<"#";
  }
  cout<<"\n";
 } 
}

//双
void f4(int n){
 int  R=n*2;//行数
 int  C0=0;//前缀空格
 int  C1=0;//中间空格
 int  C=0;//#个数(前后各一半)
 for(int row=1;row<=R;row++){
  //开始位置
  C0=(n-(abs(n+0.5-row)-0.5))-1;
  for(int i=0;i<C0;i++){
   cout<<' ';
  }
  C=(n-((abs(n+0.5-row)+0.5)))+1;
  for(int column=0;column<C;column++){
   cout<<"#";
  }
  //后面开始位置
  C1=abs(abs(n+0.5-row)+0.5)*4;
  for(int j=0;j<C1;j++){
   cout<<' ';
  }
  for(int column1=0;column1<C;column1++){
   cout<<"#";
  }
  cout<<"\n";
 } 
}

int main(int argc, char* argv[])
{

 
 print_oneline(5);
 printf("\n\n");
 print_square(5);
 printf("\n\n");
 half_triangle(6);
 printf("\n\n");
 double_half_triangle(3);
 printf("\n\n");
 anti_half_triangle(3);

 double_double_half_triangle(4);

 f4_2(3);
 f4_1(4);
 f4a(4);
 f4(4);
 return 0;

 
}

 







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值