生成并打印弗洛伊德三角形的 Java 程序
弗洛伊德三角形以罗伯特·弗洛伊德命名,是一个直角三角形,使用自然数制作而成。它从 1 开始,并按顺序选择序列中下一个更大的数字。
算法
- 取要打印的行数 n。
- 进行外部迭代 I n 次以打印行
- 对 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP