2003题目描述;等腰梯形
请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。
****
******
********
**********
输入
输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。输出
对应于m个case输出要求的等腰梯形。样例输入
2
3
4- 样例输出
#include <iostream>
using namespace std;
int main() {
int m,h,cols;
cin>>m;
while(m--) {
cin>>h;
cols = h + 2*(h-1);
int l;
for (int i = 0; i < h; i++) {
l = h+2*i; //第i行*的个数
for (int j = 0; j < cols; j++) {
if(j >= (cols-l)/2 && j <(cols-l)/2+l) cout<<'*';
else cout<<' ';
}cout<<endl;
}
}
return 0;
}
*2056题目描述:沙漏图形
问题:输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例
数据规模 1<= n <=50
- 输入样例:
3
4 - 输出样例:
#include <iostream>
using namespace std;
int main() {
int n;
while(cin>>n){
int rows = 2*n-1, count = n;
for (int i = 0; i < rows; i++) {
for (int k = count; k < n ; k++) {
cout<<' ';
}
for (int j = count; j > 0; j--) {
if(j == 1) cout<<'*'<<endl;
else cout<<"* ";
}
if (i < n-1) count--;
else count++;
}
}
return 0;
}