用JAVA如何实现杨辉三角??

孤尽JavaSea 2008-10-05 01:33:02
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

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1
...全文
6762 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
LMRT 2011-03-20
  • 打赏
  • 举报
回复
1
1 1
1 2 1
vince657885801 2011-03-03
  • 打赏
  • 举报
回复
三楼的厉害啊!牛人!用if for实现。
lan2010hong0827shi 2011-02-28
  • 打赏
  • 举报
回复

package com.test;
/**
* 递归调用实例
* @author lanhongshi
* @createDate 2011-02-28
* @modifyDate
*/
public class Recursion {
/***
* x,x+n,x+2n ... y相乘 其中n为两个数之间的间隔 ,x为起始数 y为终点数
* @param start 起始数
* @param end 终点数
* @return 最终和
*/
public int total(int start,int step,int end){
int new_start=start+step;
if(new_start<=end){
return start*total(new_start,step,end);
}
return start;
}
private int[] createThree(int currentRow,int[] temps,int startRow,int lastRow ){
if(lastRow>=currentRow){
int[] temp={};
if(currentRow==1){
temp=temps;
}else{
int colum=currentRow+1;
temp=new int[colum];//新层构建
for(int i=1;i<colum-1;i++){
if(currentRow==2){
temp[i]=temps[i-1]*2;
}else{
temp[i]=temps[i-1]+temps[i];}
}
temp[0]=1;temp[colum-1]=1;
}
if(currentRow>=startRow&¤tRow<=lastRow){
for(int j=0;j<temp.length;j++){
System.out.print(temp[j]+" ");
}
System.out.println();
}
currentRow++;
return createThree(currentRow,temp,startRow,lastRow );
}else{
return new int[]{};
}

}
/**
* 杨辉三角
* @param start 起始行
* @param end 结束行
*/
public void createYHSJ(int start,int end){
createThree(1,new int[]{1},start,end);
}
}

zhangxinjun000 2010-05-19
  • 打赏
  • 举报
回复
刚好自己今天写了一个程序。。贴出来给大家看看我的语法都有哪些不合格的地方,请大家指教。。


public class YangHui2{

public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
System.out.println("请输入一个数:");
int num=sc.nextInt();
int[][] tr = new int[num][];
System.out.println("杨辉三角为:");
//算法部分
for(int i = 0; i < tr.length; i++){
tr[i] = new int[i + 1];
tr[i][0] = 1;
tr[i][i] = 1;
for(int j = 0; j < i; j++){

if(j >= 1 && i > 1){
tr[i][j] = tr[i - 1][j - 1] + tr[i - 1][j];
}

}
}
//输出部分
for(int i = 0; i < tr.length; i++){

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

System.out.print(" ");
}
for(int j = 0; j < tr[i].length; j++){
if(tr[i][j]>=10){
System.out.print(tr[i][j]);
System.out.print(" ");
}else{

System.out.print(tr[i][j]);
System.out.print(" ");
}


}
System.out.println();

}



}

}


anzhuer 2010-05-18
  • 打赏
  • 举报
回复
好 学习下~
西安晟格奇 2010-05-11
  • 打赏
  • 举报
回复
晕 传上来就不是金字塔了 晕
西安晟格奇 2010-05-11
  • 打赏
  • 举报
回复
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 咋回事 再传次
西安晟格奇 2010-05-11
  • 打赏
  • 举报
回复
3楼的有那么复杂吗? 看我的

public class Test {
public static void main(String[] args) {
yanghui(5);
}
//杨辉三角
public static void yanghui(int n){
int b=2*n-1;
int[][] a=new int[n][b];
//0只不过是个标记
for(int i=0;i<n;i++){
for(int j=0;j<b;j++){
int x=b/2;
if(i==0 && j==x){
a[i][j]=1;
}else if(i==0){
a[i][j]=0;
}else if(i==n-1 && j==0){
a[i][j]=1;
}else if(i==n-1 && j==b-1){
a[i][j]=1;
}else if(i>0 && j>0 && j<b-1){
a[i][j]=a[i-1][j-1]+a[i-1][j+1];
}
}
}

for(int i=0;i<n;i++){
for(int j=0;j<b;j++){
//如果为0,则输出空格
if(a[i][j]==0){
System.out.print(" ");
}else{
System.out.print(a[i][j]);
}

}
System.out.println();
}

}

}
结果:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Pol 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 pjhcxlxl551122 的回复:]
三楼 高手 偶佩服
[/Quote]
写了下比较简单的。还有待优化,特别是最后的显示。

public class Yanghui{
public static void main(String args[]){
final int ROW=9;//行数,为奇数
int i,j;
int a[][];
a=new int[ROW][ROW];
for(i=0;i<ROW;i++){
a[i][i]=1;
a[i][0]=1;
}
for(i=2;i<ROW;i++)
for(j=1;j<=ROW-1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i<ROW;i++){
for(int m=1;m<33-2*i;m++)
System.out.print(" ");
for(j=0;j<=i;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}



结果:
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
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
chendabian 2008-10-06
  • 打赏
  • 举报
回复
杨辉三角最本质的特征是,它的两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。

第3行的第三个数恰好对应着两数和的平方公式(在此就不做说明了)依次下去
比如(x+y)的平方=x的平方+2xy+y的平方,这样系数就是1,2,1这就是杨辉三角的其中一行,立方,四次方,运算的结果看看各项的系数
penggaolin 2008-10-06
  • 打赏
  • 举报
回复
学习了..
VitoPJ 2008-10-05
  • 打赏
  • 举报
回复
三楼 高手 偶佩服
百年树人 2008-10-05
  • 打赏
  • 举报
回复
mark
sttou 2008-10-05
  • 打赏
  • 举报
回复
厉害
gjl_gjl 2008-10-05
  • 打赏
  • 举报
回复
学习
rushm 2008-10-05
  • 打赏
  • 举报
回复
啊。。。。MARK.
ZHANGBINFLY 2008-10-05
  • 打赏
  • 举报
回复
支持,学习
justinavril 2008-10-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
Java codepublic class Yanghui {

public static void main(String[] args) {
Yanghui yang = new Yanghui();
yang.printYanghuiTriangle(13);
}

/**
* 生成指定行数的杨辉三角形
*
* @param lines 杨辉三角形的行数
*/
public void printYanghuiTriangle(int lines) {
if(lines < 1) {
throw new IllegalArgumentException("lines mu…
[/Quote]
说来就来 还真快啊...
爱摸鱼de老邪 2008-10-05
  • 打赏
  • 举报
回复

public class YangHui

{
public static void main(String args[])

{

final int ROW=13;

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();
}
}
}
  • 打赏
  • 举报
回复
public class Yanghui {

public static void main(String[] args) {
Yanghui yang = new Yanghui();
yang.printYanghuiTriangle(13);
}

/**
* 生成指定行数的杨辉三角形
*
* @param lines 杨辉三角形的行数
*/
public void printYanghuiTriangle(int lines) {
if(lines < 1) {
throw new IllegalArgumentException("lines must be great than 0.");
}
if(lines > 30) {
throw new IllegalArgumentException("lines is too big.");
}
int[] line = new int[lines];
int maxLen = getMaxLen(lines);
for(int i = 0; i < lines; i++) {
line[0] = line[i] = 1;
for(int j = 1, k = i / 2, pre = line[0]; j <= k; j++) {
int cur = line[j];
line[i - j] = line[j] += pre;
pre = cur;
}
printLine(line, i + 1, maxLen);
}
}

/**
* 根据指定行数的杨辉三角形,计算其中最大数字的长度
* @param lines 杨辉三角形的行数
* @return 最大数字的长度
*/
private int getMaxLen(int lines) {
int k = lines / 2;
long maxNum = factorial(k + 1, lines - 1) / factorial(1, lines - 1 - k);
return getLength(maxNum);
}

/**
* 阶乘计算
* @param start 阶乘计算的起始数字
* @param num 阶乘计算的终止数字
* @return 阶乘计算结果
*/
private long factorial(int start, int num) {
long result = start > 0 ? start : 1L;
while(num > start) {
result *= num--;
}
return result;
}

/**
* 根据指定数字计算数字的长度
* @param num 数字
* @return 数字的长度
*/
private int getLength(long num) {
int len = 0;
while(num > 0L) {
num /= 10L;
len++;
}
return len;
}

private void printLine(int[] yanghui, int line, int width) {
printSpaces((yanghui.length - line) * width);

for(int i = 0; i < line; i++) {
if(i > 0) {
printSpaces(width);
}
printSpaces(width - getLength(yanghui[i]));
System.out.print(yanghui[i]);
}
System.out.println();
if(width > 1) {
System.out.println();
}
}

private void printSpaces(int spaceCount) {
for(int i = 0; i < spaceCount; i++) {
System.out.print(" ");
}
}
}

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

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1


利用二项式定理,计算出每行最大数值的位数,用以显示成三角形的形状。
为了显示成三角形的形状,这里耗费了大量的代码。
加载更多回复(2)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧