杨辉三角java源代码

来源:互联网 发布:linux脚本 双击运行 编辑:程序博客网 时间:2024/06/01 07:16
public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner input=new Scanner(System.in);
  System.out.print("请随机输入一个数字:");
  int a=input.nextInt();
  int[] triangle = null;
  int[] tmp = null;
  for (int i = 1; i <= a; i++)
  {
   tmp = new int[i];
   tmp[0] = 1;
   for (int j = 1; j < i - 1; j++)
   {
    tmp[j] = triangle[j - 1] + triangle[j];
   }
   tmp[tmp.length - 1] = 1;
   triangle = tmp;
   for (int j = 0; j < triangle.length; j++)
   {
    System.out.print(tmp[j]+"/t");
   }
   System.out.println();
  }
 }
原创粉丝点击