本题要求编写程序,打印一个高度为n的、由“*”组成的正菱形图案。
输入格式:
输入在一行中给出一个正的奇数n。
输出格式:
输出由n行星号“*”组成的菱形,如样例所示。每个星号后跟一个空格。
输入样例:
7
输出样例:
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN (512)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO {
char print_f[105];
void read() { }
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
char mtx[MAXN][MAXN];
int main() {
#ifdef debug
freopen("test", "r", stdin);
// freopen("out_main", "w", stdout);
clock_t stime = clock();
#endif
cin >> n;
int lef = n, rig = n, dx = -2, dy = 2;
for(int i=1; i<=n; i++) {
int j = 1;
// while(j<lef) mtx[i][j] = ' ';
for( ; j<=rig; j++)
mtx[i][j] = (j==lef || j==rig) ? '*' : ' ';
for(j=lef; j<=rig; j+=2)
mtx[i][j] = '*';
mtx[i][rig+1] = ' ';
mtx[i][rig+2] = 0;
if(i == n/2+1) dx = -dx, dy = -dy;
lef += dx, rig += dy;
}
for(int i=1; i<=n; i++)
printf("%s\n", mtx[i]+1);
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}