1. 程式人生 > >列印一個五層金字塔(從開發速度,便捷性,執行效能等考慮)

列印一個五層金字塔(從開發速度,便捷性,執行效能等考慮)

程式碼示例:

/**
 * 列印一個五層金字塔(從開發速度,執行效能等考慮)
 * @author ck
 */
public class testDemo {

	public static void main(String[] args) {
		planA();
		planB();
	}
	
	
	public static void planA() {
		Long a = System.currentTimeMillis();
		int i,j,k,n = 5;
		for(i=1;i<=n;i++){
            //根據外層行號,輸出星號左邊空格
			for(j=1;j<=n-i;j++)
				System.out.print(" ");//根據外層行號,輸出星號個數
			for(k=1;k<=2*i-1;k++)
				System.out.printf("*");//一行結束,換行
			System.out.printf("\n");

		}
		Long b = System.currentTimeMillis();
		System.out.println(b-a);
  }
	
	public static void planB() {
		Long a = System.currentTimeMillis();
		System.out.println("    *");
		System.out.println("   ***");
		System.out.println("  *****");
		System.out.println(" *******");
		System.out.println("*********");
		Long b = System.currentTimeMillis();
		System.out.println(b-a);
	}
}

執行結果:

1.第一次執行

2.第二次執行

3.經過20次的執行只有一次第二種方案是1

結論:

還是要看面試官的心情