关于杨辉三角形

lkof550 2007-12-01 10:28:10
这是我的代码.总达不到理想的要求
public class rect
{
public static void main(String aa[])
{
int a[][]=new int[10][10];
int i,j;
for(i=0; i<10; i++)
{
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<10; j++)
{
if(a[i][j] != 0)
System.out.println(a[i][j]);

System.out.println("\n");
}
}
}
大家写来看看
另外问一下怎么从键盘接受10个数字放到一个数组中
我是新手啊,求各位了


...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Maojm 2007-12-01
  • 打赏
  • 举报
回复
运行出错还是,没有结果啊
maxppp 2007-12-01
  • 打赏
  • 举报
回复
杨晖三角5行的。。
class Triangle4
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
for(int j=3-i;j>=0;j--)
{
System.out.print(" ");
}
for(int j=i;j>=0;j--)
{
System.out.print(times(i)/(times(j)*(times(i-j))));
System.out.print(" ");
}
System.out.print("\n");
}
}
public static int times(int n)
{
int result=1;
for(int i=1;i<=n;i++)
{
result*=i;
}
return result;
}

}
从键盘读十个数
int a[]=new int[10];
System.in.read(a);
lkof550 2007-12-01
  • 打赏
  • 举报
回复
1楼的输出不了那个三角形
2楼的编译通过就是运行不了
Maojm 2007-12-01
  • 打赏
  • 举报
回复

写的不好仅供参考。

public class test {

public static void main(String aa[]) {
int rows=10,kg;
int[] temp,ret;
ret=new int[1];
ret[0]=1;
kg=rows;
for (int i=0;i<rows;i++){
for(int ai=0;ai<kg;ai++){
System.out.print(" ");
}
for(int j=0;j<ret.length;j++)
System.out.printf("%4d",ret[j]); //注意:System.out.printf()是jdk1.5的特性。如果你是jdk1.4以
System.out.println(); //下的。会报错。你可以改成:print。不过格式比较难看。
temp=new int[ret.length+1];
if(i==0){
temp[0]=temp[1]=1;
}else{
for (int k=0;k<temp.length;k++){
if (k==0 || k==temp.length-1)
temp[k]=1;
else
temp[k]=ret[k-1]+ret[k];
}
}
ret=new int[temp.length];
ret=temp.clone();
kg--;
}

}
}
网络咖啡 2007-12-01
  • 打赏
  • 举报
回复

int[][] a = new int[10][10];
for(int i =0;i < a.length;i++){
for(int j =0;j <= i;j++){
//第一列
if(j == 0){
a[i][j] = 1;
}else{
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
}
cursor_wang 2007-12-01
  • 打赏
  • 举报
回复
import java.util.Scanner;
class Demo1{
public static void main(String[] args){
//Demo1.print(11);
System.out.println("请输入你要的级数:");
Scanner s=new Scanner(System.in);
int y=s.nextInt()+1;
Demo1.print1(y);
}

/*
*输出扬辉三角形
*/
public static void print1(int x){
int aa[][]=new int[x][x];
for(int i=0;i<x;i++){
for(int j=0;j<=i;j++){
if(i==j||j==0){
aa[i][j]=1;
}
else{
aa[i][j]=aa[i-1][j-1]+aa[i-1][j];
}
System.out.printf("%-3d ",aa[i][j]);
}
System.out.println();
}

}
}
daniel_kaka 2007-12-01
  • 打赏
  • 举报
回复

public static void printYangHui(int steps){
if (steps <= 0){
System.out.println ("杨辉三角至少执行1步!");
return;
}
int array[][] = new int[steps + 1][2*steps + 1];
for (int j = 0; j <= 2*steps; j ++){
if (j == steps){
array[0][j] = 1;
}else{
array[0][j] = 0;
}
}
for (int i = 1; i <= steps; i ++){
for (int j = 0; j <= 2*steps; j ++){
if (j == 0){
array[i][j] = array[i - 1][j + 1];
}else if(j == 2*steps){
array[i][j] = array[i - 1][j - 1];
}else{
array[i][j] = array[i - 1][j - 1] + array[i - 1][j + 1];
}
}
}
for (int i = 0; i <= steps; i ++){
for (int j = 0; j <= 2*steps; j ++){
if (array[i][j] != 0){
System.out.print(array[i][j]);
}else{
System.out.print(" ");
}
}
System.out.println();
}
}


62,623

社区成员

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

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