打印菱形第二种办法

再来一种方法

1 #include <stdio.h>
2
3  void PrintSpace(int n)
4 {
5 int i;
6 for(i=0;i<n;i++)
7 printf(" ");
8 }
9
10 void PrintStar(int n)
11 {
12 int i;
13 for(i=0;i<n;i++)
14 printf("*");
15 }
16
17 int main()
18 {
19 int spaceNum,starNum;
20 int i;//行数
21 int n;//列数
22
23 printf("Please enter the value of n:\n");
24 scanf("%d",&n);
25
26 for(i=1;i<2*n;i++)
27 {
28 //判断本行的空格数
29 if(i>n)
30 spaceNum=i-n;
31 else
32 spaceNum=n-i;
33 //判断本行的星星数
34 starNum=(n-spaceNum)*2-1;
35
36 //一行行的打印
37 PrintSpace(spaceNum);
38 PrintStar(starNum);
39 printf("\n");
40
41 }
42
43 return 0;
44 }
原文地址:https://www.cnblogs.com/fanyong/p/1984789.html