| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2776 人关注过本帖
标题:打印打印杨辉三角形怎么编程?
只看楼主 加入收藏
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
 问题点数:0 回复次数:11 
打印打印杨辉三角形怎么编程?

打印杨辉三角形(要求打印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没多久!谢谢

搜索更多相关主题的帖子: 杨辉三角形 FONT quot Roman Times 
2006-04-23 21:04
wtyl0088
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2006-3-24
收藏
得分:0 
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");

}
}
}

2006-04-23 23:17
wtyl0088
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2006-3-24
收藏
得分:0 

有点乱凑活看吧


2006-04-23 23:17
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
哦,我看下能运行出结果,可以的话请你喝酒.
2006-04-24 10:01
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
可以运行,谢谢!
2006-04-24 10:13
wtyl0088
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2006-3-24
收藏
得分:0 

酒呢 嘿嘿


2006-04-24 12:38
zhouxin
Rank: 2
等 级:新手上路
威 望:4
帖 子:76
专家分:0
注 册:2006-4-28
收藏
得分:0 
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<j;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<used;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);
}
}

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

好好编程好好学习
2006-04-29 00:17
bob7789
Rank: 1
等 级:新手上路
帖 子:132
专家分:0
注 册:2006-4-10
收藏
得分:0 

2006-04-29 16:25
xiaohui9258
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-12
收藏
得分:0 
  好几了
2007-04-03 08:00
changyawei
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-11-3
收藏
得分:0 

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输出杨辉三角形的经典程序

2007-04-03 09:23
快速回复:打印打印杨辉三角形怎么编程?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018168 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved