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 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
不知到是哪里错了请教高手打印出完整的杨辉三角
我还是新手不要用太高深的东西
就把我上面的代码改一改就行了
谢谢了
解决方案 »
- 绘制抛物线问题。 就是根据抛物线方程获得N个点,然后连接前一个和后一个点,然后作成抛物线,问题是怎么让连接的线很平滑?坐等高人指教。
- 关于字节流从socket里面读数据的问题?
- 操作oracle 写和读可以,修改和删除不能操作,请问是怎么回事?
- 小弟想学习JAVA..有经验的人进来指导一下!谢谢了.
- 执行顺序?JUnit4 注解
- 请教各位哥哥,姐姐,我是java初学者!请问java中怎么获得计算机的名字,如何更改JFrame坐上角的图标,在线等
- applet加栽有图片的控件
- 更改LAYOUT属性就报错----送分!
- 关于JAVA访问数据库两个问题
- mysql的jdbc乱码
- 一个小问题求大牛们帮解释。。在线等哈 谢谢了
- 一个小型计算器程序,还有最后一个难点。。求指教
public static void main(String args[]) {
int arr[][] = new int[7][7]; int i, j;
for (i = 1; i < 7; i++) {
for (j = 1; j <= i; j++) {
if ((i == 0) || (i == j)) {
arr[i][j] = 1;
System.out.print(arr[i][j] + " ");
if (i == 6)
break;
System.out.println("");
} else {
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
System.out.print(arr[i][j] + " ");
} } }
}
public static void main(String[] args) { int yh[][] = new int[11][21];
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 20; j++) {
if (i == 1 && j == 10) {
yh[i][j] = 1;
} else {
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();
}
}
{
public static void main(String[] args)
{
int n=10;
int yh[][]=new int[10][19];
yh[(0)][9]=1;
yh[9][0]=1;
yh[9][18]=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];
}
System.out.println();
}
for(int x=0;x<yh.length;x++)
{
for(int y=0;y<19;y++)
{
if(yh[x][y]==0)
{
System.out.print(" "+" ");
}
else
{
System.out.print(yh[x][y]+" ");
}
}
System.out.println();
}
}
}
帮忙给一下这个也行
定义一个功能函数
int[][] a = new int[10][10];
for (int i = 0; i <= 9; i++) {
a[i][0] = 1;
a[i][i] = 1;
}
for (int i = 2; i <= 9; i++)
for (int j = 1; j <= i - 1; j++)
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= i; j++) {
if (a[i][j] > 9 && a[i][j] < 100)
System.out.print(" " + a[i][j]);
if (a[i][j] > 99)
System.out.print(" " + a[i][j]);
if(a[i][j] > 0 && a[i][j] <10)
System.out.print(" " + a[i][j]);
}
System.out.println("");
}
}
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("");
}
}
}
这个就很规范啊 。