杨辉三角

杨辉三角:

import java.util.*;
public class yanghui {
         public static void main(String[] args)
         {  
                  Scanner sc = new Scanner(System.in);
                  System.out.println(“输入层数:”);
                  int cengshu = sc.nextInt();
  
                  int x[][]=new int[cengshu][cengshu];
                  int y=1;
  
                  for(int i=0;i<cengshu;i++)
                 { 
                          for(int j=0;j<y;j++)
                          {
                                   if(j==0||j==i)
                                  {
                                           x[i][j]=1;
                                   }
                                   else
                                  {
                                           x[i][j]=x[i-1][j-1]+x[i-1][j];
                                  }
                          System.out.print(x[i][j]+”\t”);
                          }
                          y++;
                         System.out.println();
                 }
         }
}

输出结果:

1
1  1
1  2  1
1  3  3  1
1  4  6  4  1
……    ……

根据输入的层数决定.

“杨辉三角”3 条评论

猪宝宝 | 2010-02-3 8:57 上午 |
commenter

怎么不是赵辉三角啊……

commenter

哇卡卡卡。。。
wordpress的插件为:http://fairyfish.net/2009/01/16/thread-twitter/

commenter

感谢大王,娃卡卡,一起推哇

发表评论:

姓名 (*):
E-Mail (*):
网址:
内容 (*):
XHTML: 可用标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>