本文目錄一覽:
Java語言楊輝三角
打印楊輝三角代碼如下:
public class woo {
public static void triangle(int n) {
int[][] array = new int[n][n];//三角形數組
for(int i=0;iarray.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]+”\t”);
}
System.out.println();
}
}
public static void main(String args[]) {
triangle(9);
}
}
擴展資料:
楊輝三角起源於中國,在歐洲這個表叫做帕斯卡三角形。帕斯卡(1623—-1662)是在1654年發現這一規律的,比楊輝要遲393年。它把二項式係數圖形化,把組合數內在的一些代數性質直觀地從圖形中體現出來,是一種離散型的數與形的優美結合。
楊輝三角具有以下性質:
1、最外層的數字始終是1;
2、第二層是自然數列;
3、第三層是三角數列;
4、角數列相鄰數字相加可得方數數列。
怎樣用java打印楊輝三角,自己輸入行
/**
* 打印楊輝三角
功能描述:使用多重循環打印6階楊輝三角
* @author pieryon
*
*/
public class YHSJ {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(“請輸入行號:”);
int m = in.nextInt();
int n = 2*m-1;//列元素數;
int arr[][] = new int[m][n];
for (int i = 0; i m; i++) { //外循環控制行
for (int j = 0; j n; j++) { //內循環控制列
if (j(m-i-1)||(j=(m+i))) { //輸出等腰三角形兩邊空格
System.out.print(” “);
}else if (j==(m-i-1)||j==(m+i-1)) { //計算輸出等腰三角形兩邊的空格
arr[i][j] = 1;
System.out.print(arr[i][j]);
}else if ((i+j)%2==0m%2==0||(i+j)%2==1m%2==1) {
System.out.print(” “);
}else {
arr[i][j] = arr[i-1][j-1]+arr[i-1][j+1];
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
}
以上就可以輕鬆實現楊輝三角的打印了!
Java算法實現楊輝三角等腰三角形
這是我寫得代碼,用得是不規則數組,可惜不是等腰三角形(本人僅是一名初中的學生,熱愛編程,個人觀點僅供參考,如有不對歡迎指正,謝謝。)
楊輝三角 java
沒貼代碼啊,給你一個我學習時寫過的吧
public static void main(String[] args)
{
int[][] pas = new int[6][];
for(int i = 0; i pas.length; i++)
{
pas[i] = new int[i + 1];
pas[i][0] = 1;
pas[i][i] = 1;
for(int j = 0; j pas[i].length – 1; j++)
{
if(j = 1 i 1)
{
pas[i][j] = pas[i – 1][j – 1] + pas[i – 1][j];
}
}
}
for(int i = 0; i pas.length; i++)
{
for(int j = 0; j pas.length – pas[i].length; j++)
{
System.out.print(” “);
}
for(int j = 0; j pas[i].length; j++)
{
System.out.print(pas[i][j]);
System.out.print(” “);
}
System.out.println();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247432.html