打印杨辉三角的java代码,打印打印杨辉三角形怎么编程?

打印打印杨辉三角形怎么编程?

打印杨辉三角形(要求打印10行)

1

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

各位大虾能不能帮忙把代码写出来,我刚学java没多久!谢谢----------------解决方案--------------------------------------------------------

public class Yanghui{

public static void main(String[] args){

int a[][];

int i=0,j;

a=new int[10][10];

for(i=0;i<10;i++)

for(j=0;j<10;j++){

a[i][0]=1;

a[i][i]=1;

}

for(i=1;i<10;i++)

for(j=1;j<=i;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i<10;i++){

for(j=0;j<=i;j++){

System.out.print(a[i][j]+" ");

}

System.out.print("\n");

}

}

}

----------------解决方案--------------------------------------------------------

有点乱凑活看吧

----------------解决方案--------------------------------------------------------

哦,我看下能运行出结果,可以的话请你喝酒.

----------------解决方案--------------------------------------------------------

可以运行,谢谢!

----------------解决方案--------------------------------------------------------

酒呢 嘿嘿

----------------解决方案--------------------------------------------------------

import java.io.*;

public class YanghuiSanjiao

{

public static void print(int i)

{

if(i<1||i>15){System.out.println("Error!");System.exit(0);

}

int[] array=new int[16];

array[0]=1;

for(int j=1;j<=i;j++)

{

int temp1=1;int temp2;

for(int k=1;k

{

temp2=array[k];

array[k]=array[k]+temp1;

temp1=temp2;

}

array[j]=1;

myprint(array,j+1);

}

}

private static void myprint(int[] a,int used)

{

for(int t=0;t

{

int n=0;

int x=a[t];

do

{

n++;

x=x/10;

}while(x!=0);

System.out.print(a[t]);

for(int l=0;l<5-n;l++)

{

System.out.print(' ');

}

}

System.out.print('\n');

}

public static void main(String[] args)throws IOException

{

BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Input the rows(no more than 15):");

int rows=Integer.parseInt(keyboard.readLine());

if (rows<1||rows>15) {System.out.println("the rows not right!");System.exit(0);

}

print(rows);

}

}

看看我这个,虽然长了点,但代码相当健壮。

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------

好几了

----------------解决方案--------------------------------------------------------

public class Yanghui {

public static void main(String args[]){

final int ROW=5;

int a[][]=new int[ROW+1][];

for(int i=0;i<=ROW;i++)

{

a[i]=new int[i+1];

}

yanghui(a,ROW);

}

static void yanghui(int a[][],int ROW) {

for(int i=0;i<=ROW;i++)

for(int j=0;j<=a[i].length-1;j++)

{

if(i==0||j==0||j==a[i].length-1)

a[i][j]=1;

else a[i][j]=a[i-1][j-1]+a[i-1][j];

}

for(int i=0;i<=ROW;i++)

{

for(int j=0;j<=a[i].length-1;j++)

System.out.print(a[i][j]+"\t");

System.out.println();

}

}

}

这是用java输出杨辉三角形的经典程序

----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值