首页 > 其他 > 详细

杨辉三角形实现

时间:2014-01-21 16:33:30      阅读:333      评论:0      收藏:0      [点我收藏+]

//打印杨辉三角形。
import java.util.Scanner;
public class Yanghui_Triangle{
      public static void main(String args[]){
           //输入行数
           Scanner scan= new Scanner(System.in);
           System.out.println("请输入杨辉三角形行数:");
           int row = scan.nextInt();
           int yanghui[][]= new int[row][];
           System.out.println("杨辉三角形");
          
           //配置二维数组
           for(int i=0;i<yanghui.length;i++){
                yanghui[i]= new int[i+1];
               }
               
               //计算杨辉三角形
               for(int n=0;n<yanghui.length;n++){
                     for(int k=0;k<=n;k++){
                           if(k==0||k==n){
                                yanghui[n][k]=1;
                               }else{
                                   yanghui[n][k]=yanghui[n-1][k-1]+yanghui[n-1][k];
                                   }
                         }
                   }
                   
                   //输出杨辉三角形
                   for(int i=0;i<yanghui.length;i++){
                         System.out.print("第"+(i+1)+"行");
                         for(int j=0;j<yanghui[i].length;j++){
                             System.out.print(yanghui[i][j]+"\t");
                             }
                             System.out.println();
                       }      
          }
    }

 

bubuko.com,布布扣

杨辉三角形实现

原文:http://www.cnblogs.com/sophine/p/3527284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!