题目:
http://oj.jzxx.net/problem.php?id=1129
程序:
#include <iostream>
using namespace std;
/*
* 因为图形上下对称,所以构造一个关于对称的预处理映射函数
* 比如,n = 5时:
* 第1行和第9行对称,第2行和第8行对称,第3行和第7行对称,第4行和第6行对称
*/
int premap(int x, int n)
{
return x >= n ? 2 * n - x : x;
}
int main()
{
int n;
cin >> n;
int width = n + 2 * (n - 1);
for(int i = 1; i <= 2 * n - 1; i++)
{
int mIndex = premap(i, n);
if(1 == mIndex) // 第一行或最后一行
{
for(int j = 1; j <= n - 1; j++)
{
cout << ' ';
}
for(int k = 1; k <= n; k++)
{
cout << '*';
}
cout << endl;
}
else
{
int j;
for(j = 1; j <= n - mIndex; j++)
{
cout << ' '; // 左边的空白
}
cout << "*"; // 左边的星号
for(int k = 1; k <= width - 2 * j; k++)
{
cout << ' '; // 中间的空白
}
cout << "*"; // 右边的星号
cout << endl;
}
}
return 0;
}
TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多内容请关注微信公众号
wechat_public_header.jpg
网友评论