首页 > 试题广场 >

翻转金字塔图案

[编程题]翻转金字塔图案
  • 热度指数:26967 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的翻转金字塔图案。

输入描述:
多组输入,一个整数(2~20),表示翻转金字塔边的长度,即“*”的数量,也表示输出行数。


输出描述:
针对每行输入,输出用“*”组成的金字塔,每个“*”后面有一个空格。
示例1

输入

5

输出

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
示例2

输入

6

输出

* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // 注意 while 处理多个 case
        while(in.hasNext()){
            int a = in.nextInt();
         
          for(int i = 0; i < a; i++){
            for(int j = 0; j < a; j++){
                if(j < i){
                    System.out.print(" ");
                }else if(0 < a-i){
                    System.out.print("* ");
                }
            }
            System.out.println("");
          }
        }      
    }
}
发表于 2024-08-30 18:35:15 回复(0)
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 = 0; i < n; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(" ");
                } //空格循环
                for (int j = n - 1 - i; j >= 0; j--) {
                    System.out.print("* ");
                } //星星循环
                System.out.println();
            }
        }
    }
}

发表于 2024-08-14 19:57:03 回复(0)
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 <= n; i++) {
                //空格
                for(int j = 1; j < i; j++) {
                    System.out.print(" ");
                }
                //符号
                for(int l = n; l >= i; l--) {
                    System.out.print("* ");
                }
                //换行
                System.out.println();
            }
        }
    }
}
发表于 2023-10-27 16:06:59 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            for(int i=1;i<=n;i++){
                for(int j=1;j<i;j++){
                    System.out.print(" ");
                }
                for(int p=n;p>=i;p--){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-11-02 16:44:27 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            int x=sc.nextInt();
            for(int i=1;i<=x;i++){
                for(int j=1;j<i;j++){
                    System.out.print(" ");
                }
                for(int k=1;k<=x+1-i;k++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-08-18 22:41:41 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            for (int s = 0; s < n; s++) {
                System.out.print("* ");
            }
            System.out.println();
            for (int i = 1; i <= n - 1; i++) { //hang
                for (int m = 0; m < i ; m++) {
                    System.out.print(" ");}
                    for (int j = n - i ; j >= 1; j--) {
                        System.out.print("* ");
                    }
                    System.out.println();
                }
            }
        }
    }

发表于 2022-07-06 11:06:19 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int num = scanner.nextInt();
            for(int i = num;i > 0;i--){
                for(int j = num - i ;j > 0 ;j--){
                    System.out.print(" ");
                }
                for(int k = 0;k < i;k++){
                    System.out.print("* ");
                }
                System.out.println("");
            }
        }
    }
}

发表于 2022-06-28 16:19:44 回复(0)
import java.util.*;
public class Main{
public static void main(String love[]){
   Scanner input=new Scanner(System.in);
    while(input.hasNextInt()){
       int n=input.nextInt();
        for(int i=n;i>0;i--){
            for(int k=n-i;k>0;k--){
                     System.out.print(" ");
                }
            for(int j=0;j<i;j++){
                System.out.print("*"+" ");
            }
             System.out.println();
        }
    }
     input.close();
} 
}

发表于 2022-05-13 13:31:46 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int a = sc.nextInt();
            for(int i=1;i<=a;i++) {
                for(int k=a;k>a-i+1;k--) {
                    System.out.print(" ");
                }
                for(int j=a;j>=i;j--) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}
发表于 2022-04-20 16:52:24 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j < i; j++) {
                    System.out.print(" ");
                }
                for (int k = i; k <= n; k++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2021-12-16 01:44:38 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            for(int i = 1; i <= n; i++){
                for(int j = 0; j < i - 1; j++){
                    System.out.print(" ");
                }
                for(int k = 1; k <= n + 1 - i; k++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2021-09-29 16:34:22 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
           Integer number = sc.nextInt();
           for(int n = 0;n<number;n++){
               for(int s = n;s>0;s--){
                   System.out.print(" ");
               }
               for(int i = number-n;i>0;i--){
                   System.out.print("* ");
               }
               System.out.println("");
           }
        }
    }
}

发表于 2021-08-18 15:56:27 回复(0)
乱搞的
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int gtr=input.nextInt();
            int empty=(gtr*2-2)/2;
            for(int i=gtr-1;i>=0;i--){
                for(int n=0;n<empty-i;n++){
                    System.out.print(" ");
                }
                for(int nn=0;nn<i+1;nn++){
                    System.out.print("* ");
                }
                System.out.println("");
            }
        }
    }
}

发表于 2020-10-30 12:26:15 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            String temp = String.join("", Collections.nCopies(n+1, " "));
            for (int i = n; i >0; i--) {
                System.out.println(String.join("", Collections.nCopies(n-i, " "))+
                        String.join(" ", Collections.nCopies(i, "*"))+" ");
            }
        }
    }
}

发表于 2020-04-22 10:03:06 回复(0)

问题信息

上传者:牛客309119号
难度:
15条回答 3222浏览

热门推荐

通过挑战的用户

查看代码
翻转金字塔图案