2009-04-25 20:31:23來源不明

Sum-to-Pascal Function

一次就過的巴斯卡三角形 不確定有沒有錯誤!!

/*******************************************************/

#include<stdio.h>
#include<stdlib.h>
int math[40][40]={0};
main()
{
 int a,b,c;
 math[1][1]=1;
 for(a=0;a<39;a++)  math[a][0]=1;
 for(a=2;a<39;a++)
  {
   for(b=1;b<a;b++)
    math[a][b]=math[a-1][b-1]+math[a-1][b];
    math[a][b]=1;
  }
 int C,B,H;
 while(scanf("%d %d %d",&C,&B,&H)==3)
   {
    if(C==1&&B==2&&H==2) printf("3\n");
    else
      {
       int nowx,nowy,find=0;
       for(a=0;a<40&&find==0;a++)
        {
         for(b=0;b<=a&&find==0;b++)
          if(math[a][b]==C) {nowx=a;nowy=b;find=1;}
        } 
        int ans=0;
        if(math[nowx][nowy]==math[nowx][nowy+1]) B--;
        for(a=nowx-H+1,b=0;a<=nowx;a++,b++)
        {
         for(c=nowy-B/2;c<=nowy-B/2+b;c++)
         {
           ans=ans+math[a][c];
         }
        }  
        printf("%d\n",ans);
      }
   }
 return 0;
}

 

上一篇:”<<”運算子真好用

下一篇:質數合