導航:首頁 > 編程語言 > java楊輝三角輸出

java楊輝三角輸出

發布時間:2022-05-22 22:28:19

『壹』 java 楊輝三角的輸出方法

//楊輝三角
public class sanjiao {
public static void main(String[] args){
int[][] num = new int [10][10];
for(int i = 0;i < num.length;i++){
for(int j = 0; j < i;j++){
if(j == 0 || i == j){
num[i][j] = 1;
}
if(i < 9 ){
num[i+1][j+1 ] = num[i][j] + num[i][j+1];
}
System.out.print(num[i][j]+" ");
}
System.out.println();
}

}
}
有什麼不懂的繼續追問。

『貳』 用JAVA程序列印出楊輝三角

publicclassTest{
publicstaticvoidmain(Stringargs[]){
inta=10;
intb[][];
b=newint[a][];
for(inti=1;i<=10;i++){
b[i-1]=newint[i];
}
for(intj=0;j<10;j++){
for(intk=0;k<=j;k++){
if(j==0||k==0||k==j)//分支
{
b[j][k]=1;//等於1的
continue;
}else{
b[j][k]=b[j-1][k-1]+b[j-1][k];//計算值
}
}
}
for(intm=0;m<10;m++){
for(intn=0;n<=m;n++){
System.out.print(b[m][n]+"");//循環輸出
}
System.out.println("");
}
}
}

『叄』 Java語言楊輝三角

列印楊輝三角代碼如下:

public class woo {

public static void triangle(int n) {

int[][] array = new int[n][n];//三角形數組

for(int i=0;i<array.length;i++){

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

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

array[i][j]=1;

}else{

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

}

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

}

System.out.println();

}

}

public static void main(String args[]) {

triangle(9);

}

}

(3)java楊輝三角輸出擴展閱讀

楊輝三角起源於中國,在歐洲這個表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年發現這一規律的,比楊輝要遲393年。它把二項式系數圖形化,把組合數內在的一些代數性質直觀地從圖形中體現出來,是一種離散型的數與形的優美結合。

楊輝三角具有以下性質:

1、最外層的數字始終是1;

2、第二層是自然數列;

3、第三層是三角數列;

4、角數列相鄰數字相加可得方數數列。

『肆』 用java編程使用二組數組方式列印楊輝三角(明天急用)

按照你的要求編寫的用二組數組方式列印楊輝三角形的Java程序如下

publicclassYangHui{
publicstaticvoidmain(String[]args){
finalintROW=5;//指定楊輝三角形的行數
inta[][]=newint[ROW+1][];
for(inti=0;i<=ROW;i++){
a[i]=newint[i+1];//指定每行的列數
}
for(inti=0;i<=ROW;i++)
for(intj=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(inti=0;i<=ROW;i++){
for(intj=0;j<=ROW-i;j++)
System.out.print("");
for(intj=0;j<=a[i].length-1;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

『伍』 java 中 最簡單楊輝三角形怎麼輸出呢

動態格式化版楊輝三角輸出:
-----------------------------------------------------------------------------------
public class Test {

private static int level = 10; // 想輸出多少行就修改這個數

public static void main(String[] args) {
int[][] arrays = new int[level][level];
int max = 0;
arrays[0][0] = 1;
for (int i = 1; i < level; i++) {
arrays[i][0] = 1;
for (int j = 1; j < i; j++) {
arrays[i][j] = arrays[i - 1][j - 1] + arrays[i - 1][j];
if (arrays[i][j] > max)
max = arrays[i][j];
}
arrays[i][i] = 1;
}

StringBuilder sb = new StringBuilder();
int maxWidth = String.valueOf(max).replaceAll("\\d", " ").length();
maxWidth += maxWidth % 2 == 0 ? 2 : 3;
String bitSpace = space(maxWidth / 2, " ");

for (int i = 0; i < level; i++) {
sb.append(space(level - i - 1, bitSpace));
for (int j = 0; j <= i; j++) {
int width = String.valueOf(arrays[i][j]).length();
int before = (maxWidth - width) / 2;
int after = maxWidth - before - width;
sb.append(String.format("%" + before + "s%d%" + after + "s", " ", arrays[i][j], " "));
}
sb.append("\n");
}
System.out.print(sb.toString());
}

private static String space(int length, String space) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
sb.append(space);
return sb.toString();
}
}
----------------------------------------------------------
5行輸出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

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
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

15行輸出:
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
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

---------------------------
HTML中顯示的空格和數字不一定是一個寬度,本代碼在Eclipse中輸出是絕對整齊的。

『陸』 輸出楊輝三角形,Java。

publicstaticvoidmain(String[]args)
{
System.out.print("請輸入整數n:");
Scannerin=newScanner(System.in);
intn=in.nextInt();
int[][]iaYong=newint[n][];//先分配n行

System.out.println("楊輝三角形:");
iaYong[0]=newint[1];//分配第一行的列
iaYong[0][0]=1;
for(inti=1;i<iaYong.length;i++)
{
iaYong[i]=newint[i+1];//分配其餘行的列
iaYong[i][0]=1;
for(intj=1;j<i;j++)
iaYong[i][j]=iaYong[i-1][j-1]+iaYong[i-1][j];
iaYong[i][i]=1;
}
for(intline=0;line<n;line++)//上半部
{
for(inti=0;i<n-line-1;i++)//輸出每行的前導空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//輸出數據
System.out.println();
}
for(intline=n-2;line>=0;line--)//下半部
{
for(inti=0;i<n-line-1;i++)//輸出每行的前導空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//輸出數據
System.out.println();
}
}

『柒』 java 輸出楊輝三角

//Yhsanjiao.java:
public class Yhsanjiao{

static public void main(String[] args){
int[][] a=new int[10][10];
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
if (j<i)
{
a[i][j]=1;
if(j==0){
a[i][j]=1;
}else{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}else{
a[i][j]=1;
}
}

for(int i=0;i<10;i++)
{
for(int k=1;k<=10-i;k++)
System.out.printf(" ");

for(int j=0;j<=i;j++){
System.out.printf("%3d ",a[i][j]);
}
System.out.printf("\n");
}
}
} 1-10的楊輝三角形

『捌』 用java編程楊輝三角的代碼

1.楊輝三角形由數字排列,可以把它看做一個數字表,其基本特性是兩側數值均為1,其他位置的數值是其正上方的數字與左上角數值之和,下面是java使用for循環輸出包括10行在內的楊輝三角形

2.思路是創建一個整型二維數組,包含10個一維數組。使用雙層循環,在外層循環中初始化每一個第二層數組的大小。在內層循環中,先將兩側的數組元素賦值為1,其他數值通過公式計算,然後輸出數組元素。
代碼如下:
public class YanghuiTriangle {
public static void main(String[] args) {
int triangle[][]=new int[10][];// 創建二維數組
// 遍歷二維數組的第一層
for (int i = 0; i < triangle.length; i++) {
triangle[i]=new int[i+1];// 初始化第二層數組的大小
// 遍歷第二層數組
for(int j=0;j<=i;j++){
// 將兩側的數組元素賦值為1
if(i==0||j==0||j==i){
triangle[i][j]=1;
}else{// 其他數值通過公式計算
triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
}
System.out.print(triangle[i][j]+"\t"); // 輸出數組元素
}
System.out.println(); //換行
}
}
}

閱讀全文

與java楊輝三角輸出相關的資料

熱點內容
二次元表格編程 瀏覽:20
plc編程器保停 瀏覽:963
如何降低伺服器的內存佔用率 瀏覽:868
阿里雲伺服器是個什麼意思 瀏覽:817
國內最好的數控編程培訓學校 瀏覽:13
222乘104列是演算法 瀏覽:159
程序員溝通正確姿勢 瀏覽:969
魔玩app怎麼視頻推廣 瀏覽:960
程序員抽獎送禮 瀏覽:458
北京java程序員薪資 瀏覽:658
如何創建網路平台或者app 瀏覽:355
python隨機數生成控制概率 瀏覽:235
壓縮機並聯運行 瀏覽:899
兩位單片機 瀏覽:63
四川音樂類投檔線的演算法 瀏覽:650
建行app如何改轉賬卡 瀏覽:26
android開發升級 瀏覽:299
要火社區app進不去怎麼辦 瀏覽:826
安卓手機上的自定義功能怎麼用 瀏覽:230
方舟伺服器怎麼購買進去資格 瀏覽:44