杨辉三角形(像等边三角形一样)输出

杨辉三角形(像等边三角形一样)输出,第1张

public static void printStr77() {

int i, j;

int h = 7;

int yanghui[][] = new int[7][];

Systemoutprintln("杨辉三角形");

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

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

yanghui[0][0] = 1;

}

for (i = 1; i < yanghuilength; i++) {

yanghui[i][0] = 1;

for (j = 1; j < yanghui[i]length - 1; j++) {

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

}

yanghui[i][yanghui[i]length - 1] = 1;

}

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

printBlack(yanghuilength - i);

for (j = 0; j < yanghui[i]length; j++)

Systemoutprint(yanghui[i][j] + " ");

Systemoutprintln();

}

}

public static void printBlack(int count){

for (int i = 0 ; i < count ; i ++) {

Systemoutprint(" ");

}

}

但是如果数字超过两位数三位数时,还是会出问题的。

这个要先指定行数然后确定每个字的输出规则,挺麻烦的。

本程序只使用了一维数组

public classYangHui{

public static void main(String args[])

{ int i;

int yh[]=new int[8]; //一行中最大八个元素

for(i=0;i<8;i++) //i实际控制着行数,即当前打印到第i行

{ yh[i]=1; //初始化数组中的当前行能用到的每个元素为1(比如当前打印到第4行,则初始化数组中前4个元素为1,此处较难理解,其实前3个元素已经在前面的循环中初始化,所以此处实际是初始化一行中最后一个元素为1)

for(int j=i-1;j>0;j--) //第3行才开始本循环

yh[j]=yh[j-1]+yh[j]; //赋值前yh[j]实际上是前一行的数据,即[i-1]行的数据,所以此处重新赋值为yh[j-1]+yh[j],赋值后yh[j]才变成当前行需要的数据。此处看懂了,整个程序就看懂了!

for(int j=0;j<=i;j++) //此循环只是输出一行中的数据而已

Systemoutprint(yh[j]+"\t");

Systemoutprintln();} //一行输出完后换行

}

}

本程序比较巧妙,我也是看了好几遍才看明白,真正难点只在于赋值那一行。

public class Yanghui {

public Yanghui() {

}

//用来在每一次打印前面加空格, 看的整齐

public static String addSpace(int i) {

String str = StringvalueOf(i);

int len = strlength();

for (int j = 0; j < 4 - len; j++) {

str = " " + str;

}

return str;

}

public static void main(String args[]) {

int i, j, n = 10;

int mat[][] = new int[n][];

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

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

mat[i][0] = 1;

mat[i][i] = 1;

int matLen = mat[i]length - 2;

//只对每行的元素个数超过2个的才进行循环赋值

if(matLen > 0)

{

for (j = 0; j < matLen; j++)

{

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

}

}

}

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

for (j = 0; j < mat[i]length; j++)

//需要其它不同的打印方式, 可以自己调试的加上去

Systemoutprint(addSpace(mat[i][j]));

Systemoutprintln();

}

}

}

import javautilScanner;

public class YTriangular {

public void run(int num) {

if (num < 3) {

main(null);

}

int[][] yt = new int[num][num];

for (int i = 0; i < num; i++) {

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

if (i == j || j == 0) {

yt[i][j] = 1;

} else {

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

}

Systemoutprint(yt[i][j] + " ");

}

Systemoutprintln("");

}

}

public static void main(String[] args) {

Systemoutprintln("请输入行数(>=3)");

Scanner scan = new Scanner(Systemin);

int num = scannextInt();

new YTriangular()run(num);

}

}

public static int f(int a; int b){

if(a==b||b==1) return 1;

return f(a-1,b-1)+f(a-1,b);

}

如果坐标是从1开始输的话 那么第六行第四个数应该是10 而不是5

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

… … … … …

public class YH{

public static void main(String args[])

{ int i;

int yh[]=new int[8];

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

{ yh[i]=1;

for(int j=i-1;j>0;j--)

yh[j]=yh[j-1]+yh[j];

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

Systemoutprint(yh[j]+"\t");

Systemoutprintln();}

}

}

以上就是关于杨辉三角形(像等边三角形一样)输出全部的内容,包括:杨辉三角形(像等边三角形一样)输出、java用一维数组编写杨辉三角,我不是很明白,请高手指点。、java的杨辉三角问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9330581.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存