题目描述
请输出下列形状:
*
* *
* *
* *
*
Copy
输入输出格式
输入格式
无输入
输出格式
参见题目描述
输入输出样例
輸入資料 1
无输入
Copy
輸出資料 1
*
* *
* *
* *
*
Copy
这道题有两种方法,先来看第一种:
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<" *"<<endl;
cout<<" * *"<<endl;
cout<<"* *"<<endl;
cout<<" * *"<<endl;
cout<<" *"<<endl;
return 0;
}
接下来,是使用for循环:(不建议使用,输出有变)
#include<iostream>
using namespace std;
int main()
{
int n = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < (n - 1) - i; j++)
{
cout << " ";
}
for (int j = 0; j < (2 * i + 1); j++)
{
cout << "*";
}
cout << endl;
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < i + 1; j++)
{
cout << " ";
}
for (int j = 0; j < 2 * ( n - 1 ) - (1 + 2 * i); j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}