TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(9)杨辉三角形

程序:
- public class Test9{
- public static void main(String args[]){
- int n=Integer.parseInt(args[0]);
- System.out.println("N="+n);
- for(int i=0;i<=n;i++){
- for(int j=0;j< 12-i;j++) System.out.printf(" ");
- for(int j=1;j< i+2;j++) System.out.printf("%6d",c(i,j));
- System.out.println();
- }
-
- }
- public static int c(int x,int y){//求杨辉三角形中第X行第Y列的值。
- int z;
- if((y==1)||(y==x+1)) return 1;
- z=c(x-1,y-1)+c(x-1,y);
- return z;
- }
- }
-
- 运行:
- [img]http://img.javaxxz.com/2014/11/25/000423171.jpg[/img]
-
复制代码
源码下载:http://file.javaxxz.com/2014/11/25/000423281.zip |
|