按指定的方式打印输出回环金字塔数列。
示例:
n = 3
输出:
1
3*2
4*5*6
n = 7
输出:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
21*20*19*18*17*16
22*23*24*25*26*27*28
public class HelloWorld {
public static void main(String []args) {
int n = 7;
int c = 1;
for (int i = 1; i <= n; i++)
{
int d = c;
if (i % 2 == 0)
{
for (int j = d + i - 1; j >= d; j--)
{
if (j != d + i - 1) System.out.print("*");
System.out.print(j);
c++;
}
}
else
{
for (int j = d; j < d + i; j++)
{
if (j != d) System.out.print("*");
System.out.print(j);
c++;
}
}
System.out.println();
}
}
}
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
21*20*19*18*17*16
22*23*24*25*26*27*28
package com.thinkgem.jeesite.test;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入层高");
int floor = sc.nextInt();
Test.getStringArray(1, floor);
}
public static void getStringArray(int first, int n) {
int start = first * (first - 1) / 2 + 1;
if (first % 2 == 0) {
for (int i = start + first - 1; i >= start; i--) {
System.out.print(i + "\t");
}
} else {
for (int i = start; i < start + first; i++) {
System.out.print(i + "\t");
}
}
System.out.println();
if (first == n) {
return;
}
getStringArray(++first, n);
}
}