该题目为模拟题,
注意从数组首位同时进行赋值会使算法时间复杂度降低
代码:
#include <cstdio>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn = 101;
char matrix[maxn][maxn];
int n;
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 0; i < 101; i++)
{
for(int j = 0; j < 101; j++)
{
matrix[i][j] = '*';
}
}
int top_i = 0;
int bottom_i = n - 1;
int sign_num = 1;
int start_position = n/2;
while(top_i != bottom_i)
{
int temp_start_position = start_position;
while(start_position < temp_start_position + sign_num)
{
matrix[top_i][start_position] = 'D';
matrix[bottom_i][start_position] = 'D';
start_position++;
}
top_i++;
bottom_i--;
sign_num += 2;
start_position = temp_start_position;
start_position--;
}
for(int i = 0; i < n; i++)
{
matrix[top_i][i] = 'D';
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("%c",matrix[i][j]);
}
printf("\n");
}
}
return 0;
}