日期:2014-05-20 浏览次数:20901 次
package cn.dzr.testnumber;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
P("请输入要打印的的杨辉三角的层数: (最小为3)\n");
Scanner s = new Scanner(System.in);
int i;
while((i = s.nextInt())<3)
{
P("请重新输入..\n");
}
s.close();
int[][] arr =new int[12][];
arr[0] = new int[1];
arr[0][0] = 1;
arr[1] = new int[2];
arr[1][0] = 1;
arr[1][1] = 1;
for(int k =2;k<i;k++)
{
arr[k] = new int[k+1];
for(int j=1;j<=k/2;j++)
{
arr[k][0] = 1;
arr[k][k] = 1;
arr[k][j] = arr[k-1][j]+arr[k-1][j-1];
arr[k][k-j] = arr[k][j];
}
}
//输出
for(int m=0;m<i;m++)
{
for(int t=0;t<i-m;t++)
{
P(" ");
}
for(int val:arr[m])
{
P(val);
P(" ");
}
P("\n");
}
}
public static <T> void P(T t)
{
System.out.print(t);
}
}