首页 > 试题广场 >

金字塔图案

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

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


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

输入

4

输出

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

输入

5

输出

    * 
   * * 
  * * * 
 * * * * 
* * * * *
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 = 0; j < n; j++) {
                    if (j < n - i - 1) {
                        System.out.print(" "); //控制空格数即可
                    } else {
                        System.out.print("* ");
                    }
                }
                System.out.println();
            }

        }
        in.close();
    }
}

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

import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            for (int i = 0; i < Integer.parseInt(str); i++) {
                for (int j = Integer.parseInt(str); j > i + 1; j--) {
                    System.out.print(" "); //中间一个空格就是金字塔,两个空格就是BC102题答案
                }
                for (int k = 0; k < i + 1; k++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2023-10-11 17:12:44 回复(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=n-1;j>=i;j--){
                    System.out.print(" ");
                }
                for(int p=1;p<=i;p++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-11-02 15:36:05 回复(0)
import java.util.Scanner;

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

发表于 2022-08-18 22:33: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 i = 1; i <= n; i++) {//hang
                for (int j = n - i; j >= 1; j--) {
                    System.out.print(" ");
                }
                for (int m = 1; m <= i; m++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-07-06 10:39:44 回复(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 = 0;i < num;i++){
                for(int j = num - i ;j > 1 ;j--){
                    System.out.print(" ");
                }
                for(int k = 0;k <= i;k++){
                    System.out.print("* ");
                }
                System.out.println("");
            }
        }
    }
}

发表于 2022-06-28 16:14:57 回复(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=0;i<n;i++){
            for(int k=n-i;k>1;k--){
                     System.out.print(" ");
                }
            for(int j=0;j<=i;j++){
                System.out.print("*"+" ");
            }
             System.out.println();
        }
    }
     input.close();
} 
}

发表于 2022-05-13 13:27:04 回复(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 = 1; j <= n-i; j++){
                    System.out.print(" ");
                }
                for(int k = 1; k <= i; k++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2021-09-29 16:20:59 回复(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 = 1;n<=number;n++){
               for(int s = number-n;s>0;s--){
                   System.out.print(" ");
               }
               for(int i = n;i>0;i--){
                   System.out.print("* ");
               }
               System.out.println("");
           }
        }
    }
}

发表于 2021-08-18 15:56:49 回复(0)
如果提示空格或换行错误,多提交几次或等会儿再试一下就可以了,这tm是什么bug
发表于 2021-07-26 18:15:50 回复(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=0;i<gtr;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:22:28 回复(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 = 1; i <=n; i++) {
                //输出n-i个空格" ",以及拼接字符串->在i个*中插入空格" "
                //Collections.nCopies(i, "*") 是复制i-1个*,如i=3,就是***
                //下面的String.join方法是在字符串空隙中插入指定字符串,如***,变成* * *
                System.out.println(String.join("", Collections.nCopies(n-i, " "))+
                String.join(" ", Collections.nCopies(i, "*"))+" ");
            }
        }
    }
}

编辑于 2020-07-17 21:57:10 回复(3)