日期:2014-05-20  浏览次数:20627 次

新手的一个小题 求高手给改造完整
class YangHui
{
public static void main(String[] args)
{
int n=10;
int yh[][]=new int[10][19];
yh[(0)][yh[9].length-10]=1;
for(int i=1;i<10;i++)
{
for(int j=1;j<18;j++)
{
yh[i][j]=yh[i-1][j-1]+yh[i-1][j+1];

if(yh[i][j]==0)
{
System.out.print(" ");
}
else
{
System.out.print(yh[i][j]+" ");
}
}
System.out.println("");
}
}
}


打印出来的图形是这样的


E:\JavaProject\Project>java YangHu
  1 1
  1 2 1
  1 3 3 1
  1 4 6 4 1
  1 5 10 10 5 1
  1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
 9 36 84 126 126 84 36 9
 

高手应该懂的我想打印什么了 
第一行少了一个1
最后一行的第一个元素喝醉后一个元素也少了1
不知到是哪里错了请教高手打印出完整的杨辉三角 
我还是新手不要用太高深的东西
就把我上面的代码改一改就行了 
谢谢了

------解决方案--------------------
public class YangHui {
public static void main(String[] args) {
int n = 10;
int yh[][] = new int[n][2*n+2];
yh[0][n] = 1;
for (int i = 0; i < n; i++){
for (int j = 0; j <2*n+1; j++){
if(i != 0&&j !=0 ){
yh[i][j] = yh[i-1][j-1] + yh[i-1][j+1];
}
StringBuffer sb =new StringBuffer();
sb.append(" ");
if (yh[i][j]==0){

sb.setLength(4);
System.out.print(sb.toString());
} else {
String str =String.valueOf(yh[i][j]);
sb.setLength(4-str.length());
System.out.print(str+ sb.toString());
}
}
System.out.println("");
}
}
}

------解决方案--------------------
有答案了就结贴吧。。。呵呵

探讨

呃。。。11楼的 。。。不是上面那个 复制过来就不是原来的样子了 应该试听困难的 先不学了 以有问题就问你了哈 师傅。。。