close

巴斯卡三角形
Pascal's Triangle


0 1 0 0 0 0
0 1 1 0 0 0
0 1 2 1 0 0
0 1 3 3 1 0
0 1 4 6 4 1

計算原理:如上面文字所列
每一格的數值即是( x , y )  ,其中 y 是由上往下數
套入公式  ( x , y ) = ( x-1 , y -1 ) + ( x , y-1 )        
使用陣列,馬上就能寫出來

Every number existed in the triangle can be expressed like ( x , y ) = ( x - 1 , y -1 ) + ( x , y - 1 )
Please notice that the " y " here , is counted from up to down.
( For example : ( 2 , 1 ) = 0      ( 2 , 2 ) = 1      ( 3 , 3 ) = 3  )
You can try to write a script to compute this triangle using the formula above. ( It's recommended to use array. )


在數學上的應用:
Usage of Pascal's Triangle in Mathematics


原始碼 (使用最省記憶、最有效率的寫法)

#include<stdio.h>
#include<stdlib.h>

int  main ()

{
     int phase[34][2],counter,x,row,y;
       
    printf("  《楊輝三角形產生器》\n\n本程式可以輸出你輸入的列數\n\n任何時候都可以輸入-1離開\n\n(32位元電腦只支援到34行)\n\n ");
   
    while(x==x)/* 無限迴圈開始 */
    {         
     for(x=0;x<=34;x++) /* 清空變數 */
    {
      for(y=0;y<=1;y++)
        phase[x][y]=0;
    }
       
     printf("請輸入顯示列數 : ");
     scanf("%d",&counter);
    
     printf("\n");

     if(counter>=35)
       counter=34;

     if(counter==-1) /* 使用者輸入 -1 離開 */
        break;
             
     phase[0][0]=1; /* 初始化計算用數值 */
    
     row=1;
    
       while(row<=counter) /* 程式跑到指定列數才結束 */
       {                                     
           for(x=1;x<=counter-row;x++) /* 美化用的迴圈... */
            printf(" ");

        for(x=1;x<=row;x++) /* 計算並顯示數值 */
        {
            phase[x][1]=phase[x-1][0]+phase[x][0];
            printf(" %d",phase[x][1]);
      
        }
       
          for(x=0;x<=row;x++) /* 放入這個迴圈的數值 讓下個迴圈計算 */
            phase[x][0]=phase[x][1];
      
       row++; /* 跑的列數 +1 */
    
       printf("\n\n");   
       }
     } /* 無限迴圈結束 */
     printf("(c) NTPU CSIE 49571602\n\n");
    
     system("pause");
    
     return 0;

Pascal's Triangle Code. ( White Texts Hidden Below, for your reference. )
(Using the most efficient method, which saves most memory that I've ever tried.)
#include<stdio.h>
#include<stdlib.h>

int  main ()

{
     int phase[34][2],counter,x,row,y;
       
    printf("  << Pascal's Triangle Computer >>\n\nThis program computes the number of line you requested.\n\nYou may exit any time by typing -1\n\n( A 32-bit computer is limited to display less than 34 lines. )\n\n ");
   
    while(x==x)
    {         
     for(x=0;x<=34;x++)
    {
      for(y=0;y<=1;y++)
        phase[x][y]=0;
    }
       
     printf("For How many Lines would you like to Compute : ");
     scanf("%d",&counter);
    
     printf("\n");

     if(counter>=35)
       counter=34;

     if(counter==-1)
        break;
             
     phase[0][0]=1;
    
     row=1;
    
       while(row<=counter)
       {                                     
           for(x=1;x<=counter-row;x++)
            printf(" ");

        for(x=1;x<=row;x++) /* 計算並顯示數值 */
        {
            phase[x][1]=phase[x-1][0]+phase[x][0];
            printf(" %d",phase[x][1]);
      
        }
       
          for(x=0;x<=row;x++)
            phase[x][0]=phase[x][1];
      
       row++;
    
       printf("\n\n");   
       }
     }
     printf("(c) NTPU CSIE 49571602\n\n");
    
     system("pause");
    
     return 0;




arrow
arrow
    全站熱搜
    創作者介紹
    創作者 finalfrank 的頭像
    finalfrank

    Frank's 資訊科技潮流站

    finalfrank 發表在 痞客邦 留言(0) 人氣()