3875 - 4.17 显示金字塔
时间限制 : 1 秒
内存限制 : 128 MB
编写程序,提示用户输入一个在1到15之间的整数,然后显示一个金字塔形状的图案。如下面的运行示例所示:
7

题目输入
输入一个在1到15之间的整数
题目输出
显示一个金字塔形状的图案。每个数字之间空一格,最后一行的第一个数字顶格。如下面形状:

输入/输出样例
输入格式
7
输出格式
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
Java解答
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner n = new Scanner(System.in); int a = n.nextInt(); int i = 1, k = 0, y = 0; if (a < 10) { while (i <= a) { k = (a - i) * 2; while (k > 0) { System.out.print(" "); k--; } y = 0; while (i > 0) { System.out.print(i + " "); i--; y++; } i += 2; while (i <= y) { System.out.print(i + " "); i++; } System.out.print("\n"); } } else { int b = a - 9; while (i <= 9) { k = (9 - i) * 2 + b * 3; while (k > 0) { System.out.print(" "); k--; } y = 0; while (i > 0) { System.out.print(i); System.out.print(" "); i--; y++; } i += 2; while (i <= y) { System.out.print(i); System.out.print(" "); i++; } System.out.print("\n"); } b = a - 10; while (i <= a) { k = (a - i) * 2 + b; while (k > 0) { System.out.print(" "); k--; } b--; y = 0; while (i > 0) { System.out.print(i); System.out.print(" "); i--; y++; } i += 2; while (i <= y) { System.out.print(i); System.out.print(" "); i++; } System.out.print("\n"); } } } }