这个思想是很先进,所以分享了,大家可以参考一下,
import java.util.Scanner;
/**
* 输出的结果为为下所示:
* 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
* @author 王新
*
*/
public class Test {
public static long combi(int n, int r) {
long p = 1;
//思想就是在每一行的开始第一个数字是1,然后第二个数字是前一个数字乘以行数-1,再除以循环的次数就行可以了
for (int i = 1; i <= r; i++)
p = p * (n - i + 1) / i;
return p;
}
public static void show(int n) {
long tem ;
for (int i = 0; i
for (int j = 0; j
if (j == 0)
for (int k = 0; k <= (n - i); k++)
System.out.print(" ");
else
System.out.print(" ");
tem = combi(i,j);
if(tem!=0)
System.out.print(tem+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入层数:");
int n = sc.nextInt();
show(n);
}
}