hdu 2031 杨辉三角

时间:2023-02-12 08:08:38

题意:显示杨辉三角形。

解法:

组合数学公式:combi(n,m)=combi(n-1,m-1)+combi(n-1,m);

至于为什么有这个公式呢?那就是高中数学的内容啦

   1:  #include<stdlib.h>
   2:  #include<string.h>
   3:  #include<stdio.h>
   4:  #define N 31
   5:  int map[N][N];
   6:  void init(){
   7:      int i,j;
   8:      memset(map,0,sizeof(map));
   9:      map[0][0]=1;
  10:      for(i=1;i<N;i++){
  11:          for(j=1;j<=i;j++){
  12:              map[i][j]=map[i-1][j-1]+map[i-1][j];  //关键代码
  13:          }
  14:      }
  15:  }
  16:  int main(){
  17:      int n,i,j,cnt=0;
  18:      init();
  19:      while(scanf("%d",&n)!=EOF){
  20:          cnt++;
  21:          
  22:          for(i=1;i<=n;i++){
  23:              for(j=1;j<=i;j++){
  24:                  if(j!=1) printf(" ");
  25:                  printf("%d",map[i][j]);
  26:              }
  27:              printf("\n");
  28:          }
  29:          printf("\n");
  30:      }
  31:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }