程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 一步一步寫算法(之“數星星”)

一步一步寫算法(之“數星星”)

編輯:關於C語言

 

【 聲明:版權所有,歡迎轉載,請勿用於商業用途。  聯系信箱:feixiaoxing @163.com】

 

 

 

    學過編程的朋友都知道,當初為了學習編程語言中的各種語法結構,我們要試著解決各種各樣奇怪的題目。其中“數星星”就似乎其中的一種。什麼是“數星星”呢?就是打印各種形狀的“*”,正三角、倒三角、菱形等等。本篇博客純粹為了紀念我們逝去的歲月。

 

    a)正三角

 

 

void star_1() 

    int outer; 

    int inner; 

 

    for(outer = 1; outer <10; outer ++){ 

        for(inner = 1; inner <= outer; inner ++){ 

            printf("%c ", '*'); 

        } 

        printf("\n"); 

    } 

void star_1()

{

       int outer;

       int inner;

 

       for(outer = 1; outer <10; outer ++){

              for(inner = 1; inner <= outer; inner ++){

                     printf("%c ", '*');

              }

              printf("\n");

       }

}    b)倒三角

 

 

void star_2() 

    int outer; 

    int inner; 

     

    for(outer = 9; outer >= 1; outer --){ 

        for(inner = 1; inner <= outer; inner ++){ 

            printf("%c ", '*'); 

        } 

        printf("\n"); 

    } 

void star_2()

{

       int outer;

       int inner;

      

       for(outer = 9; outer >= 1; outer --){

              for(inner = 1; inner <= outer; inner ++){

                     printf("%c ", '*');

              }

              printf("\n");

       }

}  c)左三角

 

 

 

void star_3() 

    int outer; 

    int inner; 

     

    for(outer = -4; outer <= 4; outer ++){ 

        for(inner = 1; inner <= (5 - abs(outer)); inner ++){ 

            printf("*"); 

        } 

 

        for(; inner <= 5; inner ++){ 

            printf(" "); 

        } 

 

        printf("\n"); 

    } 

void star_3()

{

       int outer;

       int inner;

      

       for(outer = -4; outer <= 4; outer ++){

              for(inner = 1; inner <= (5 - abs(outer)); inner ++){

                     printf("*");

              }

 

              for(; inner <= 5; inner ++){

                     printf(" ");

              }

 

              printf("\n");

       }

}    d) 右三角

 

 

void star_4() 

    int outer; 

    int inner; 

     

    for(outer = -4; outer <= 4; outer ++){ 

        for(inner = 1; inner <= abs(outer); inner ++){ 

            printf(" "); 

        } 

         

        for(; inner <= 5; inner ++){ 

            printf("*"); 

        } 

         

        printf("\n"); 

    } 

void star_4()

{

       int outer;

       int inner;

      

       for(outer = -4; outer <= 4; outer ++){

              for(inner = 1; inner <= abs(outer); inner ++){

                     printf(" ");

              }

             

              for(; inner <= 5; inner ++){

                     printf("*");

              }

             

              printf("\n");

       }

}    e)垂直菱形

 

 

void star_5() 

    int outer; 

    int inner; 

     

    for(outer = -4; outer <= 4; outer ++){ 

        for(inner = -4; inner <= abs(outer) -5; inner ++){ 

            printf(" "); 

        } 

         

        for(; inner <= 5 - abs(outer); inner ++){ 

            printf("*"); 

        } 

         

        printf("\n"); 

    } 

void star_5()

{

       int outer;

       int inner;

      

       for(outer = -4; outer <= 4; outer ++){

              for(inner = -4; inner <= abs(outer) -5; inner ++){

                     printf(" ");

              }

             

              for(; inner <= 5 - abs(outer); inner ++){

                     printf("*");

              }

             

              printf("\n");

       }

}    f)水平菱形

 

 

void star_6() 

    int outer; 

    int inner; 

     

    for(outer = -9; outer <= 0; outer ++){ 

        for(inner = outer; inner < 0; inner ++){ 

            printf(" "); 

        } 

         

        for(; inner <= 9; inner ++){ 

            printf("*"); 

        } 

         

        printf("\n"); 

    } 

void star_6()

{

       int outer;

       int inner;

      

       for(outer = -9; outer <= 0; outer ++){

              for(inner = outer; inner < 0; inner ++){

                     printf(" ");

              }

             

              for(; inner <= 9; inner ++){

                     printf("*");

              }

             

              printf("\n");

       }

}

 

    g)楊輝三角形

 

 

void star_7() 

    int outer; 

    int inner; 

     

    for(outer = 1; outer <10; outer ++){ 

        for(inner = 1; inner <= outer; inner ++){ 

            printf("%3d ", outer * inner); 

        } 

        printf("\n"); 

    } 

void star_7()

{

       int outer;

       int inner;

      

       for(outer = 1; outer <10; outer ++){

              for(inner = 1; inner <= outer; inner ++){

                     printf("%3d ", outer * inner);

              }

              printf("\n");

       }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved