输出星型,如:
******* ***** *** * *** ***** *******
C code:
// 用法如:a.exe 7 #include<stdio.h> int main(int argc, char *argv[]) { if(argc < 2) { printf("Usage: %s <n>\n", argv[0]); } else { int n = atoi(argv[1]); int i = 0, j; for(i = n; i >= 1; i -= 2) { for(j = 0; j < (n - i) / 2; j++) // 空格 printf(" "); for(j = 0; j < i; j++) // * printf("*"); printf("\n"); } for(i += 4; i <= n; i += 2) { for(j = 0; j < (n - i) / 2; j++) printf(" "); for(j = 0; j < i; j++) printf("*"); printf("\n"); } } return 0; }
运行结果:
[zcm@t #26]$./a 7 ******* ***** *** * *** ***** ******* [zcm@t #27]$./a 9 ********* ******* ***** *** * *** ***** ******* ********* [zcm@t #28]$./a 11 *********** ********* ******* ***** *** * *** ***** ******* ********* *********** [zcm@t #29]$./a 21 ********************* ******************* ***************** *************** ************* *********** ********* ******* ***** *** * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* [zcm@t #30]$./a 20 ******************** ****************** **************** ************** ************ ********** ******** ****** **** ** **** ****** ******** ********** ************ ************** **************** ****************** ******************** [zcm@t #31]$./a 2 ** [zcm@t #32]$./a 10 ********** ******** ****** **** ** **** ****** ******** ********** [zcm@t #33]$