使用 java 数组打印金字塔数列
在 java 中打印从 1 到 n 的金字塔数列可以使用数组实现。具体步骤如下:
示例代码:
import static java.util.Arrays.toString; public class Solution { public static void main(String[] args) { int[] series = arithSeries(5); System.out.println(toString(series)); // [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5] } public static int[] arithSeries(int n) { int l = (n * (n + 1)) / 2; int[] ser = new int[l]; int pointer = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { ser[pointer] = j; pointer++; } } return ser; } }