From 772a8f0600adcc24e9a7958a79c69b05dfa1e2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=9B=E9=AA=8F=E6=B4=8B?= <836157478@qq.com> Date: Sat, 5 Aug 2023 04:08:07 +0000 Subject: [PATCH] =?UTF-8?q?update=20problems/0012.=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E5=9B=BE=E5=BD=A2.md.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 盛骏洋 <836157478@qq.com> --- ...60\345\255\227\345\233\276\345\275\242.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0012.\346\211\223\345\215\260\346\225\260\345\255\227\345\233\276\345\275\242.md" "b/problems/0012.\346\211\223\345\215\260\346\225\260\345\255\227\345\233\276\345\275\242.md" index e5ecb4e..73b01d7 100644 --- "a/problems/0012.\346\211\223\345\215\260\346\225\260\345\255\227\345\233\276\345\275\242.md" +++ "b/problems/0012.\346\211\223\345\215\260\346\225\260\345\255\227\345\233\276\345\275\242.md" @@ -60,7 +60,38 @@ int main() { } ``` ## Java +```java +import java.util.Scanner; +public class Main{ + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + while (in.hasNextInt()) { + int n = in.nextInt(); + for (int i = 1; i < 2 * n; i++) { //第1行到第2n-1行 + for (int j = 0; j < Math.abs(n - i); j++) { + System.out.print(" "); //空格 + } + int j = 1; + if (i <= n) { //上半部分(包括第n行) + for (; j <= i; j++) { + System.out.print(j); //每一行中的正序部分,从1打印到i + } + } else { //下半部分(不包括第n行) + for (; j <= 2 * n - i; j++) { + System.out.print(j); + } + } + for (j = j - 2; j >= 1; j--) { //每一行中的倒叙部分 j-2是因为上面的j最后多加了一次 + System.out.print(j); + } + System.out.println(); + } + } + } +} + +``` ## python ```python -- Gitee