2044 - 星号三角形
时间限制 : 1 秒
内存限制 : 128 MB
打印用*号表示的等腰三角形,接收输入一个整数指明三角形高度
题目输入
输入一个整数n(n <= 30),以EOF结束输入
题目输出
输出高度为n的三角形,每打印完一个三角形都要换行,参见样例输出
输入/输出样例
输入格式
1 3 2
输出格式
* * *** ***** * ***
C语言解答
#include<stdio.h> main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { for(int j=1;j<=n+i;j++) { if(j<n-i) printf(" "); else printf("*"); } printf("\n"); } } }
C++解答
#include <iostream> int main() { using namespace std; char star = '*'; int height, weight; while (cin >> height){ weight = (2 * height - 1) / 2; for (int i = 1; i <= height; i++){ for (int j = 1; j <= weight; j++) cout << " "; for (int j = 1; j <= 2*i-1; j++) cout << star; cout << endl; weight--; } } }