急急急!!!!!C语言:利用二维数组输出如下图形 ******* ***** *** * *** ***** *******

******* ***** *** * *** ************
2024-12-23 12:32:04
推荐回答(2个)
回答1:

cat PrintStar.c
#include

int main(void)
{
int iUpRow, iDownRow;
int i;
int nMaxLine;
int nStar, nSpace;

Again:
printf("最大行*号个数(只能为单):");
scanf("%d", &nMaxLine);
if (nMaxLine % 2 == 0)
goto Again;
iUpRow = nMaxLine / 2 + 1;
iDownRow = nMaxLine - iUpRow;

for (i = 0; i < iUpRow; ++i)
{
nSpace = iUpRow - i - 1;
for (; nSpace; --nSpace)
printf(" ");
nStar = 2 * i + 1;
for (; nStar; --nStar)
printf("*");
printf("\n");
}

#if 1
for (i = 0; i < iDownRow; ++i)
{
nSpace = i + 1;
for (; nSpace; --nSpace)
printf(" ");
nStar = iDownRow * 2 - 2 * i - 1;
for (; nStar; --nStar)
printf("*");
printf("\n");
}
#endif
return 0;
}

回答2:

#include
#define N 7
main()
{
char a[N][N];
int i,j;
// 1/4 up_left
for(i=0;i<=N/2;i++)for(j=0;j<=N/2;j++)
if(j>=i)a[i][j]='*';
else a[i][j]=' ';
// copy up_left to down_left
for(i=N-1;i>=N/2;i--)for(j=0;j<=N/2;j++)
a[i][j]=a[N-1-i][j];
// copy left to right
for(i=0;iN/2;j--)
a[i][j]=a[i][N-1-j];
// printf a[N][N]
for(i=0;i for(j=0;j putchar('\n');
}
}
*******
*****
***
*
***
*****
*******