Java 程式可生成並列印弗洛伊德三角形
弗洛伊德三角形,是以羅伯特·弗洛伊德命名的直角三角形,它是使用自然數構建的。從 1 開始,然後在序列中連續選擇下一個較大數。
演算法
- 為待列印的行數取一個數 n。
- 對於 n 次,進行外部迭代 I 以列印行
- 對於 J 進行內部迭代以獲取 I
- 列印 K
- 遞增 K
- 在每次內部迭代後列印換行符
示例
import java.util.Scanner;
public class FloyidsTriangle {
public static void main(String args[]){
int n,i,j,k = 1;
System.out.println("Enter the number of lines you need in the FloyidsTriangle");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i = 1; i <= n; i++) {
for(j=1;j <= i; j++){
System.out.print(" "+k++);
}
System.out.println();
}
}
}輸出
Enter the number of lines you need in the FloyidsTriangle 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP