C++用*号输出一个梯形……得用cout语句,不得用C语言的print语句
看到题目,觉得想试一下就试了,仅供参考:
#include <iostream>using namespace std;
/**
* 输出一个由*号构成的等腰梯形,
* 参数:bottom 底边*号的个数
* height 梯形的高度(行数)
* 注意:bottom 必须为自然奇数,如果为偶数n,底边将自动变为 n+1
* height 必须为 <= bottom/2 (int) 如果如果输入不合法将自动设置为 bottom/2(int)
*/
void trapezoid(int bottom,int height)
{
int half = bottom / 2;
if(height<2 || height>half) height = half;
cout << "底边:" << half*2+1 << "高:" << height <<endl<<endl;
for( int i = half-height+1; i <= half; i++)
{
for(int j=half-i;j>0;j--)cout << " ";
for(int j=2*i+1;j>0;j--)cout << "*";
cout << endl;
}
cout << endl;
}
int main()
{
//测试数据
trapezoid(21,4);
trapezoid(20,12);
trapezoid(19,3);
trapezoid(18,4);
return 0;
}
输出结果:
底边:21高:4***************
*****************
*******************
*********************
底边:21高:10
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
底边:19高:3
***************
*****************
*******************
底边:19高:4
*************
***************
*****************
*******************
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-10-11
你要正着的还是倒着的,等腰还是直角,底多长高多长?直角最简单了,等腰你要也行,你看着办追问
正着的,等腰,底长高随你喜好!🙏拜托
追答嗯
#include
using namespace std;
int bl=5,t=2;
for(int i=0;i<=10;i++)
{
for(int b=0;b<=bl;++b)
cout<<" ";
for(int tx=0;tx<=t;tx++)
cout<<"*";
cout<<endl;
bl--;
t++;
}