列印菱形第二種辦法

來源:互聯網
上載者:User

再來一種方法

 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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.