实验思路:编写函数输出指定行数的星星等腰三角形,首先用for循环来控制行数,再利用行数和左边的空格数相加等于5,写出第二个for循环关系,再利用星星的个数等于2乘以行数-1的关系写出第三个for循环,当星星写完后,就用endl跳向下一格。
#include <iostream>
using namespace std;
void Star(int n);
int main(){
int a;
cout << "Please enter the number of rows " << endl;
cin >> a;
cout <<"The star of outputs is " << endl;
Star(a);
return 0;
}
void Star(int n){
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n - i; j++)
cout << ' ';
for(int j = 1; j <= 2*i-1; j++)
cout << '*';
cout << endl;
}
}