去面试有一个杨辉三角的输出,以前上学的时候弄过,那时候觉得挺难的,尤其是递归,感觉很难,现在想想也不是难么难了,不过还是出了点小问题,忘输出换行了,记起了以前刚接触代码的时候也是,老忘记换行,这么多年了,毛病还依然存在啊。总之一句话,就是欠练。
贴代码:
public static void main(String[] args) {
int [] b = new int[]{1};
Test t = new Test();
t.fun(b,9);
} private void fun(int[] b, int count) {
int[] a ;
if(count ==0 )
return;
if(count != 1){
for(int i =0;i<count-1;i++){
System.out.print(" ");
}
}
for(int i =0;i<b.length;i++){
System.out.print(b[i] + " ");
}
System.out.println();
if(b.length ==1){
a = new int[]{1,1};
} else {
a = new int[b.length +1];
a[0] = 1;
for(int i = 1;i<b.length;i++){
a[i] = b[i-1]+b[i];
}
a[b.length] =1;
}
b = null;
fun(a,count-1);
}
贴代码:
public static void main(String[] args) {
int [] b = new int[]{1};
Test t = new Test();
t.fun(b,9);
} private void fun(int[] b, int count) {
int[] a ;
if(count ==0 )
return;
if(count != 1){
for(int i =0;i<count-1;i++){
System.out.print(" ");
}
}
for(int i =0;i<b.length;i++){
System.out.print(b[i] + " ");
}
System.out.println();
if(b.length ==1){
a = new int[]{1,1};
} else {
a = new int[b.length +1];
a[0] = 1;
for(int i = 1;i<b.length;i++){
a[i] = b[i-1]+b[i];
}
a[b.length] =1;
}
b = null;
fun(a,count-1);
}
int line = 行数
int[][] array = new int[line][];
for(int i=0;i<line;i++){
array[i] = new int[i+1];
}
然后按规律依次填充