我试图绘制这颗钻石,以便在中间行之后,钻石的其余部分向右移动3个空格,就像你通过一杯水看它一样.你能以某种方式修改下面的代码来结合这种转变吗?
输出应该类似于:
*
***
*****
*******
*****
***
*
Run Code Online (Sandbox Code Playgroud)
(但中间最多有15颗星).
谢谢!
define TOTAL_ROWS 15
int main() {
int row, col, numstars=1;
int half, rate=1;
for (row=1; row<=TOTAL_ROWS; row++) {
half=TOTAL_ROWS/2;
for (col=0; col<half+1-numstars; col++)
printf(" ");
for (col=1; col<=2*numstars-1; col++)
printf("*");
if (numstars==(half+1))
rate = -rate;
numstars+=rate;
printf("\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)