1. 程式人生 > >華為機試題(練習)

華為機試題(練習)

目錄

1、選秀節目打分

2、奇偶排序

3、作業系統任務排程問題。

4. 列印陣列中最大的2個數

5.迴文數字判斷。 

6.中級題:亮著電燈的盞數  

7.高階題:地鐵換乘

8.判斷if語句括號是否合法

9. 列印 楊輝三角形

 

1、選秀節目打分

分為專家評委和大眾評委,score[] 數組裡面儲存每個評委打的分數,judge_type[] 裡儲存與 score[] 陣列對應的評委類別,judge_type[i] == 1,表示專家評委,judge_type[i] == 2,表示大眾評委,n表示評委總數。打分規則如下:專家評委和大眾評委的分數先分別取一個平均分(平均分取整),然後,總分 = 專家評委平均分  * 0.6 + 大眾評委 * 0.4,總分取整。如果沒有大眾評委,則 總分 = 專家評委平均分,總分取整。函式最終返回選手得分。

            函式介面   int cal_score(int score[], int judge_type[], int n) 

package com.huawei;

import java.util.Arrays;

/*
 * @author yuzongtao 
 */
public class XuanXiu {

	/**
	 * 1、選秀節目打分,分為專家評委和大眾評委,score[] 數組裡面儲存每個評委打的分數,
	 * judge_type[] 裡儲存與 score[] 陣列對應的評委類別,
	 * judge_type[i] == 1,表示專家評委,judge_type[i] == 2,表示大眾評委,n表示評委總數。
	 * 打分規則如下:專家評委和大眾評委的分數先分別取一個平均分(平均分取整),
	 * 然後,總分 = 專家評委平均分  * 0.6 + 大眾評委 * 0.4,總分取整。
	 * 如果沒有大眾評委,則 總分 = 專家評委平均分,總分取整。函式最終返回選手得分。
	 * 函式介面  int cal_score(int score[], int judge_type[], int n) 
	 */
	public static void main(String[] args) {
		int n = 10;  // n表示評委總數
		int[] score = new int[n];
		int[] judge_type = new int[n]; 
		for(int i=0;i<10;i++){
			score[i] = (int) (100*Math.random());  // score[] 數組裡面儲存每個評委打的分數
			judge_type[i] = i%2 == 0 ? 1 : 2;   // judge_type[] 裡儲存與 score[] 陣列對應的評委類別:1,表示專家評委,2,表示大眾評委 
		}
		
		int totalScore = cal_score(score,judge_type,n); 
		System.out.println("score="+Arrays.toString(score));
		System.out.println("judgeTypes="+Arrays.toString(judge_type));
		System.out.println("totalScore="+totalScore);
	}
	
	public static int cal_score(int score[], int judge_type[], int n){ 
		int totalScore = 0; // 選手總分
		if(null != judge_type && judge_type.length>0){ 
			int zNum = 0;  // 專家人數
			int dNum = 0;  // 大眾人數
			int zTotalScore = 0; // 專家總分 
			int dTotalScore = 0; // 專家總分   
			for(int i=0;i<judge_type.length;i++){
				 if(judge_type[i] == 2){ 
					 zTotalScore += score[i];
					 zNum++;
				 }else{
					 dTotalScore += score[i];
					 dNum++;
				 }
			 } 
			if(dTotalScore > 0){  // 總分 = 專家評委平均分  * 0.6 + 大眾評委 * 0.4,總分取整。
				totalScore = (int) (Math.round(zTotalScore/zNum)*0.6 + Math.round(dTotalScore/dNum)*0.4);
			}else{  //  總分 = 專家評委平均分,總分取整
				totalScore = (int) Math.round(zTotalScore/zNum);
			}
			
		}
		return totalScore;
	}

}

輸出:

score=[93, 39, 21, 42, 83, 13, 96, 34, 34, 82]
judgeTypes=[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
totalScore=51

2、奇偶排序

給定一個數組input[] ,如果陣列長度n為奇數,則將陣列中最大的元素放到 output[] 陣列最中間的位置,如果陣列長度n為偶數,則將陣列中最大的元素放到 output[] 陣列中間兩個位置偏右的那個位置上,然後再按從大到小的順序,依次在第一個位置的兩邊,按照一左一右的順序,依次存放剩下的數。

      例如:input[] = {3, 6, 1, 9, 7}   output[] = {3, 7, 9, 6, 1};             input[] = {3, 6, 1, 9, 7, 8}    output[] = {1, 6, 8, 9, 7, 3}

             函式介面   void sort(int input[], int n, int output[])

package com.huawei;

import java.util.Arrays;

/*
 * @author yuzongtao 
 */
public class SortTest {

	/**
	 * 給定一個數組input[] ,如果陣列長度n為奇數,則將陣列中最大的元素放到 output[] 陣列最中間的位置,
	 * 如果陣列長度n為偶數,則將陣列中最大的元素放到 output[] 陣列中間兩個位置偏右的那個位置上,
	 * 然後再按從大到小的順序,依次在第一個位置的兩邊,按照一左一右的順序,依次存放剩下的數。
	 * 
	 * 例如:input[] = {3, 6, 1, 9, 7}       output[] = {3, 7, 9, 6, 1}; 
	 *     input[] = {3, 6, 1, 9, 7, 8}    output[] = {1, 6, 8, 9, 7, 3}
	 *     
	 * 函式介面 void sort(int input[], int n, int output[])    
	 */
	public static void main(String[] args) { 
	    int[] input = {3, 6, 1, 9, 7};
	    int[] output = new int[input.length];
	    sort(input,input.length,output);
	    
	    System.out.println(" ----------------------");
	    
	    int[] input2 = {3, 6, 1, 9, 7, 8};
	    int[] output2 = new int[input2.length];
	    sort(input2,input2.length,output2);
	    
	} 
	
	public static void sort(int input[], int n, int output[]){ 
		boolean flag = false;  // 奇數(false)  偶數(true)
 		flag = n%2 == 1 ? false : true ;
 		Arrays.sort(input);  // input資料 正向排序
		System.out.println("input="+Arrays.toString(input));
 		if(!flag){ // 如果陣列長度n為奇數,則將陣列中最大的元素放到 output[] 陣列最中間的位置  
 			for(int i=0;i<input.length;i++){
 				if(i%2 == 0){   // 中間第奇數個數字排在右邊
 					output[(n+i)/2] = input[input.length-1-i];
 				}else{  // 中間第偶數個數字排在左邊
 					output[(n-i)/2-1] = input[input.length-1-i];
 				} 
 			}
 			System.out.println("output="+Arrays.toString(output));
 		}else{ // 如果陣列長度n為偶數,則將陣列中最大的元素放到 output[] 陣列中間兩個位置偏右的那個位置上
 			for(int i=0;i<input.length;i++){
 				if(i%2 == 0){   // 陣列中間兩個位置偏右的那個位置上
 					output[(n+i)/2] = input[input.length-1-i];
 				}else{  // 陣列中間兩個位置偏左的那個位置上
 					output[(n-i-1)/2] = input[input.length-1-i];
 				} 
 			}
 			System.out.println("output="+Arrays.toString(output));
 		} 
	}
}

輸出:

input=[1, 3, 6, 7, 9]
output=[3, 7, 9, 6, 1]
 ----------------------
input=[1, 3, 6, 7, 8, 9]
output=[1, 6, 8, 9, 7, 3]

3、作業系統任務排程問題。

作業系統任務分為系統任務和使用者任務兩種。其中,系統任務的優先順序 < 50,使用者任務的優先順序 >= 50且 <= 255。優先順序大於255的為非法任務,應予以剔除。現有一任務佇列task[],長度為n,task中的元素值表示任務的優先順序,數值越小,優先順序越高。函式scheduler實現如下功能,將task[] 中的任務按照系統任務、使用者任務依次存放到 system_task[] 陣列和 user_task[] 陣列中(陣列中元素的值是任務在task[] 陣列中的下標),並且優先順序高的任務排在前面,優先順序相同的任務按照入隊順序排列(即先入隊的任務排在前面),陣列元素為-1表示結束。

      例如:task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99}    system_task[] = {0, 3, 1, 7, -1}    user_task[] = {4, 8, 2, 6, -1}

             函式介面    void scheduler(int task[], int n, int system_task[], int user_task[])

package com.huawei;

import java.util.Arrays; 

/*
 * @author yuzongtao 
 */
public class schedulerTest {

	/**
	 * 作業系統任務排程問題。作業系統任務分為系統任務和使用者任務兩種。
	 * 其中,系統任務的優先順序 < 50,使用者任務的優先順序 >= 50且 <= 255。優先順序大於255的為非法任務,應予以剔除。
	 * 現有一任務佇列task[],長度為n,task中的元素值表示任務的優先順序,數值越小,優先順序越高。
	 * 函式scheduler實現如下功能,將task[] 中的任務按照系統任務、使用者任務依次存放到 system_task[] 陣列
	 * 和 user_task[] 陣列中(陣列中元素的值是任務在task[] 陣列中的下標),並且優先順序高的任務排在前面,
	 * 優先順序相同的任務按照入隊順序排列(即先入隊的任務排在前面),陣列元素為-1表示結束。
	 * 例如:task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99}    
	 *     system_task[] = {0, 3, 1, 7, -1}    user_task[] = {4, 8, 2, 6, -1}
	 * 函式介面  void scheduler(int task[], int n, int system_task[], int user_task[])
	 */
	public static void main(String[] args) { 
		
		int[] task = {0, 30, 155, 1, 80, 300, 170, 40, 99};
		int[] system_task = null;
		int[] user_task = null;
		scheduler(task, task.length, system_task, user_task);
	}
	
	public static void scheduler(int task[], int n, int system_task[], int user_task[]){  
		int sys_length = 0;   // 系統任務陣列長度
		int user_length = 0;  // 使用者任務陣列長度
		for(int i=0;i<task.length;i++){
			if(task[i]<50){
				sys_length++;
			}
			
			if(task[i]>=50 && task[i]<=255){ 
				user_length++;
			} 
		} 
		
		int[] sys_value = new int[sys_length]; // 系統任務值的陣列
		int[] user_value = new int[user_length];  // 使用者任務值的陣列
		system_task = new int[sys_length+1]; // 系統任務下標的陣列
		user_task = new int[user_length+1]; // 使用者任務下標的陣列
		int sys_index = 0;  // 系統任務下標
		int user_index = 0;  // 使用者任務下標
		for(int i=0;i<task.length;i++){
			if(task[i]<50){  // 獲取系統任務 值和下標的陣列
				system_task[sys_index] = i;  // 下標
				sys_value[sys_index] = task[i];  // 值
				sys_index++;
			}
			
			if(task[i]>=50 && task[i]<=255){  // 獲取使用者任務 值和下標的陣列
				user_task[user_index] = i;   // 下標
				user_value[user_index] = task[i];  // 值
				user_index++;
			}  
		}
		 
		for(int i=0;i<sys_value.length;i++){
			for(int j=i;j<sys_value.length;j++){
				if(sys_value[i]>sys_value[j]){
					// 系統任務陣列值排序
					int sys_num = sys_value[i];
					sys_value[i] = sys_value[j];
					sys_value[j] = sys_num;
					
					// 系統任務陣列下標排序
					int temp = system_task[i]; 
					system_task[i] = system_task[j];
					system_task[j] = temp;
				}
			} 
		}
		
		for(int i=0;i<user_value.length;i++){
			for(int j=i;j<user_value.length;j++){
				if(user_value[i]>user_value[j]){
					// 使用者任務陣列值排序
					int user_num = user_value[i];
					user_value[i] = user_value[j];
					user_value[j] = user_num;
					
					// 使用者任務陣列下標排序
					int temp = user_task[i]; 
					user_task[i] = user_task[j];
					user_task[j] = temp;
				}
			} 
		}
		system_task[system_task.length-1] = -1;
		user_task[user_task.length-1] = -1;
		System.out.println("task="+Arrays.toString(task));
		System.out.println("system_task="+Arrays.toString(system_task));
		System.out.println("user_task="+Arrays.toString(user_task));
	} 
}

輸出:

task=[0, 30, 155, 1, 80, 300, 170, 40, 99]
system_task=[0, 3, 1, 7, -1]
user_task=[4, 8, 2, 6, -1]

4. 列印陣列中最大的2個數

   手動輸入一個儲存整數的陣列,要求輸出數組裡面的2個最大值。例項: 輸入:1,2,5,9,84,3,2      輸出:84,9

package com.huawei;

import java.util.Arrays;

/*
 * @author yuzongtao 
 */
public class FindMaxTwoNum {
	/**
	 * 手動輸入一個儲存整數的陣列,要求輸出數組裡面的2個最大值。
	 * 例項: 輸入:1,2,5,9,84,3,2輸出:84,9
	 */
	public static void main(String[] args) { 
		int[] array = {1,22,3,56,34,5,0,567};
		Arrays.sort(array); 
		System.out.println("最大的兩個數:"+array[array.length-1]+","+array[array.length-2]); 
	}

}

輸出:

最大的兩個數:567,56

5.迴文數字判斷。 

    有這樣一類數字,他們順著看和倒著看是相同的數,例如:121,656,2332等,這樣的數字就稱為:迴文數字。編寫一個函式,判斷某數字是否是迴文數字。

package com.huawei;

/*
 * @author yuzongtao 
 */
public class HuiWenShuTest {

	/**
	 * 迴文數字判斷
	 * 有這樣一類數字,他們順著看和倒著看是相同的數,
	 * 例如:121,656,2332等,這樣的數字就稱為:迴文數字。
	 * 編寫一個函式,判斷某數字是否是迴文數字。
	 */
	public static void main(String[] args) {
		int num = 123321;
		boolean flag = isPalindrome(String.valueOf(num));
		System.out.println("num = 123321 是否為迴文數:"+flag);

	}

	public static boolean isPalindrome(String strIn){
		boolean flag = false;
		String str = "";
		char[] strIns = strIn.toCharArray();
		for(int i=strIns.length;i>0;i--){
			str += strIns[i-1];
		}
		if(strIn.equals(str)){
			flag = true;
		}
		return flag;
	}
}

輸出:

num = 123321 是否為迴文數:true

6.中級題:亮著電燈的盞數  

一條長廊裡依次裝有n(1 ≤ n ≤ 65535)盞電燈,從頭到尾編號1、2、3、…n-1、n。每盞電燈由一個拉線開關控制。開始,電燈全部關著。有n個學生從長廊穿過。第一個學生把號碼凡是1的倍數的電燈的開關拉一下;接著第二個學生把號碼凡是2的倍數的電燈的開關拉一下;接著第三個學生把號碼凡是3的倍數的電燈的開關拉一下;如此繼續下去,最後第n個學生把號碼凡是n的倍數的電燈的開關拉一下。n個學生按此規定走完後,長廊裡電燈有幾盞亮著。注:電燈數和學生數一致。

package com.huawei;

/*
 * @author yuzongtao 
 */
public class LightTest {

	/**
	 * 亮著電燈的盞數      
	 * 一條長廊裡依次裝有n(1 ≤ n ≤ 65535)盞電燈,從頭到尾編號1、2、3、…n-1、n。每盞電燈由一個拉線開關控制。
	 * 開始,電燈全部關著。有n個學生從長廊穿過。第一個學生把號碼凡是1的倍數的電燈的開關拉一下;
	 * 接著第二個學生把號碼凡是2的倍數的電燈的開關拉一下;接著第三個學生把號碼凡是3的倍數的電燈的開關拉一下;
	 * 如此繼續下去,最後第n個學生把號碼凡是n的倍數的電燈的開關拉一下。n個學生按此規定走完後,長廊裡電燈有幾盞亮著。
	 * 注:電燈數和學生數一致。
	 */
	public static void main(String[] args) { 
		int n = 100;
		int[] lights = new int[n];
		int[] students = new int[n];  
		for(int i=0;i<students.length;i++){
			for(int j=0;j<lights.length;j++){
				if((j+1)%(i+1) == 0){ 
					if(lights[j] == 0){
						lights[j] = 1;  // 1 代表燈亮  
					}else{
						lights[j] = 0;  // 1 代表燈滅  
					}  
				}
			} 
		}  
		
		int num = 0;
		for(int i=0;i<lights.length;i++){
			if(lights[i] == 1){
				num++;
			} 
		}
		System.out.println("長廊裡電燈亮著的燈數目為:"+num);
	}

}

輸出:

長廊裡電燈亮著的燈數目為:10

7.高階題:地鐵換乘


已知2條地鐵線路,其中A為環線,B為東西向線路,線路都是雙向的。經過的站點名分別如下,兩條線交叉的換乘點用T1T2表示。編寫程式,任意輸入兩個站點名稱,輸出乘坐地鐵最少需要經過的車站數量(含輸入的起點和終點,換乘站點只計算一次)。
地鐵線A(環線)經過車站:

A1 A2 A3  A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18

地鐵線B(直線)經過車站:
B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15

package com.huawei;
 
import java.util.Scanner;

/*
 * @author yuzongtao 
 */
public class StationTest {

	/**
	 * 高階題:地鐵換乘
	 * 已知2條地鐵線路,其中A為環線,B為東西向線路,線路都是雙向的。經過的站點名分別如下,兩條線交叉的換乘點用T1、T2表示。
	 * 編寫程式,任意輸入兩個站點名稱,輸出乘坐地鐵最少需要經過的車站數量(含輸入的起點和終點,換乘站點只計算一次)。
	 * 地鐵線A(環線)經過車站:A1 A2 A3  A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
	 * 地鐵線B(直線)經過車站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
	 */
	public static void main(String[] args) { 
		// 1.輸入2個站點名稱
		System.out.println("請輸入地鐵站名稱:");
		Scanner sc = new Scanner(System.in);
		String[] stationNames = sc.nextLine().split(",");
		String[] stationALine = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","T1","A10",
				"A11","A12","A13","T2","A14","A15","A16","A17","A18"};
		String[] stationBLine = {"B1","B2","B3","B4","B5","T1","B6","B7","B8","B9","B10","T2","B11","B12","B13","B14","B15"};
		int num = 0; // 距離地鐵站數目
		// 2.計算2個站點分別在哪條地鐵線上
		String startStation = stationNames[0];
		String endStation = stationNames[1];
		boolean sA = isInLine(stationALine,startStation);
		boolean sB = isInLine(stationBLine,startStation);
		boolean eA = isInLine(stationALine,endStation);
		boolean eB = isInLine(stationBLine,endStation);
		 
		// 3.計算最少經過的車站數量
		// 3.1  如果在一條地鐵站上,只需要計算一條線上的距離,不用考慮換乘,因為只允許換乘一次
		// 3.2 如果分別在2條地鐵線路上
		if(sA && eA){  // 如果都在A地鐵上
			num = getIndex(stationALine,endStation) - getIndex(stationALine,startStation);
		}else if(sB && eB){ // 如果都在B地鐵上
			num = getIndex(stationBLine,endStation) - getIndex(stationBLine,startStation);
		}else{
			int sAT1 = Math.abs(getIndex(stationALine,"T1") - getIndex(stationALine,startStation)+1);  // 起點到T1站距離
			int sAT2 = Math.abs(getIndex(stationALine,"T2") - getIndex(stationALine,startStation)+1);  // 起點到T2站距離
			int sBT1 = Math.abs(getIndex(stationBLine,"T1") - getIndex(stationBLine,endStation)+1);   // 終點點到T1站距離
			int sBT2 = Math.abs(getIndex(stationBLine,"T2") - getIndex(stationBLine,endStation)+1);   // 終點點到T2站距離
			num = sAT1 + sBT1 > sAT2 +sBT2 ? sAT2 +sBT2 : sAT1 + sBT1;  // 取最短距離
		}
		
        System.out.println("最少需要經過的車站數量:"+Math.abs(num+1));
	}
	
	// 計算2個站點分別在哪條地鐵線上
	public static boolean isInLine(String[] stationLine,String stationName){
		boolean flag = false;
		for(int i=0;i<stationLine.length;i++){
			if(stationLine[i].equals(stationName)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	// 獲取地鐵站所在陣列下標
	public static int getIndex(String[] stationLine,String stationName){ 
		int index = 0;
		for(int i=0;i<stationLine.length;i++){
			if(stationLine[i].equals(stationName)){
				index = i;
				break;
			}
		}
		return index;
	}

}

輸出:

請輸入地鐵站名稱:
A3,B9
最少需要經過的車站數量:12

8.判斷if語句括號是否合法

程式設計的時候,if條件裡面的“(”、“)”括號經常出現不匹配的情況導致編譯不過,請編寫程式檢測輸入一行if語句中的圓括號是否匹配正確。同時輸出語句中出現的左括號和右括號數量,如if((a==1)&&(b==1))是正確的,而if((a==1))&&(b==1))是錯誤的。注意if語句的最外面至少有一對括號。提示:用堆疊來做。

輸入:if((a==1)&&(b==1))

輸出:RIGTH 3 3

輸入:if((a==1))&&(b==1))

package com.huawei;

import java.util.Scanner;
import java.util.Stack;

/*
 * @author yuzongtao 
 */
public class kuoHaoMatchTest {

	/**
	 * 括號配對問題
	 * 判斷if語句括號是否合法
	 * 程式設計的時候,if條件裡面的“(”、“)”括號經常出現不匹配的情況導致編譯不過,請編寫程式檢測輸入一行if語句中的圓括號是否匹配正確。
	 * 同時輸出語句中出現的左括號和右括號數量,如if((a==1)&&(b==1))是正確的,而if((a==1))&&(b==1))是錯誤的。
	 * 注意if語句的最外面至少有一對括號。
	 * 提示:用堆疊來做。
	 * 輸入:if((a==1)&&(b==1))   輸出:RIGHT 3 3
	 * 輸入:if((a==1))&&(b==1))  輸出:WRONG 3 4
	 */
	public static void main(String[] args) { 
		System.out.println("請輸入測試程式段:");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String msg = isMatch(str); 
		System.out.println(msg);
	}
	
	public static String isMatch(String str){
		String isMatch = "RIGHT"; 
		int numLeft = 0;   // 左括號數目
		int numRight = 0;  // 右括號數目
		Stack<String> st = new Stack<String>();
		if(null != str && !"".equals(str)){ 
			
			// 校驗左右括號是否匹配 
			for(int i=0;i<str.length();i++){
				if('(' == str.charAt(i)){  // 遇到左括號就放入棧中
					st.push(String.valueOf(str.charAt(i)));  
					numLeft++;	
				}else if(')' == str.charAt(i)){  // 遇到右括號就取出棧頂元素進行比較
					if(!st.isEmpty() && "(".equals(st.peek())){   
						st.pop();   // 如果棧頂元素為"(",則刪除棧頂元素
					}else{
						isMatch = "WRONG";
					}
					numRight++;
				} 
			}
		}
		return isMatch+" "+numLeft+" "+numRight;
	}

}

輸出:

請輸入測試程式段:
if((a==1)&&(b==1)) 
RIGHT 3 3
請輸入測試程式段:
if((a==1))&&(b==1))
WRONG 3 4

9. 列印 楊輝三角形

有如下規律:
     1.左右兩邊都是1
     2.有n行那麼第n行就有n個數
     3.每行除了左右兩邊的數之外,第n行第l列的數字是第n-1行的l-1與第n-1行的l數之和

三種實現方式,第一種是我自己寫的,與第二種類似,第三種最簡潔

package com.huawei;

public class YangHuiSanJiao {

	/**
	 * 有如下規律:
	 * 1.左右兩邊都是1
	 * 2.有n行那麼第n行就有n個數
	 * 3.每行除了左右兩邊的數之外,第n行第l列的數字是第n-1行的l-1與第n-1行的l數之和
	 */
	public static void main(String[] args) { 
		System.out.println("第一種實現方式:"); 
		int n=10; // 定義楊輝三角行數
		int[][] yhArray = new int[n][n];
		// 迴圈賦值得到楊輝三角的二維陣列
		for(int i=0;i<yhArray.length;i++){
			int[] array = new int[i+1];  // 有n行那麼第n行就有n個數,左右兩邊都是1
			array[0] = 1;   
			array[array.length-1] = 1;
			for(int j=1;j<array.length-1;j++){
				// 每行除了左右兩邊的數之外,第n行第l列的數字是第n-1行的l-1與第n-1行的l數之和
				array[j] = yhArray[i-1][j-1] + yhArray[i-1][j]; 
			} 
			yhArray[i] = array;
		}
		
		// 列印等腰楊輝三角
		for(int i=0;i<yhArray.length;i++){
			int[] array = yhArray[i];
			//列印空格,第一行列印 n-1個空格,第二行n-2個空格
            for(int m = 0; m < yhArray.length - 1 - i; m++){
                System.out.print("  ");
            }
			for(int j=0;j<array.length;j++){
				System.out.printf("%4d",array[j]);
			}
			System.out.println(""); 
		} 
		
		System.out.println(""); 
		System.out.println("==================================================");  
		System.out.println("第二種實現方式:");   
		
		//定義二維陣列的長度
        int length = 10;
        //宣告二維陣列
        int[][] arr = new int[length][];
        //遍歷二維陣列
        for(int i = 0; i < arr.length; i++){
            //列印空格
            for(int m = 0; m < arr.length - 1 - i; m++){
                System.out.print("  ");
            }
            //給每個二維資料的元素賦值一維陣列
            arr[i] = new int[i+1];
            //遍歷一維陣列
            for(int j = 0; j < arr[i].length; j++){
                //第一個元素和最後一個元素的值都是1
                if( j == 0 || j == arr[i].length -1 ){
                    arr[i][j] = 1;
                }else{
                    //當前一維陣列的索引n元素的值,等於前一個數組索引n-1,加上索引n的值
                    arr[i][j] = arr[i -1][j - 1] + arr[i - 1][j];
                }
                //格式化輸出元素值
                System.out.printf("%4d",arr[i][j]);
            }
            //換行
            System.out.println();  
        }
        
        System.out.println(""); 
		System.out.println("==================================================");  
		System.out.println("第三種實現方式:"); 
		
        int rows = 10; 
        for(int i =0;i<rows;i++) {
            int number = 1;
            //列印空格字串
            System.out.format("%"+(rows-i)*2+"s","");
            for(int j=0;j<=i;j++) {
                 System.out.format("%4d",number);
                 number = number * (i - j) / (j + 1);                
            }
            System.out.println();
        }

	}

}

輸出:

第一種實現方式:
                     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
                   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
                     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