PAT Level B 1027, with question analysis and code comments

1027 Print hourglass (20 points)

This question requires you to write a program to print a given symbol into an hourglass shape. For example, given 17 "*", it is required to print in the following format

*****
 ***
  *
 ***
*****
     
    

The so-called "hourglass shape" means that each line outputs an odd number of symbols; the centers of the symbols in each line are aligned; the number of symbols in two adjacent lines differs by 2; the number of symbols first decreases from large to small to 1, and then increases in order from small to large; the first and last symbols The numbers are equal.
Given any N symbols, they may not exactly form an hourglass. The printed hourglass is required to use as many symbols as possible.

Input format:

The input gives a positive integer N (≤1000) and a symbol on one line, separated by spaces.

Output format:

First print the largest hourglass shape consisting of the given symbols, and finally print the number of remaining unused symbols on a line.

Input example:

19 *
   
    

Output sample:

*****
 ***
  *
 ***
*****
2

Question analysis:
Use a while true loop to find the number of each layer and put it into the list collection. According to the number of each digit in the collection, output the corresponding number of symbols. At the same time, define the variable count to output the space in front of the symbol.

AC code:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner s=new Scanner(System.in);
        int N=s.nextInt();
        char c=s.next().charAt(0);//符号
        int count=0,sum=1;
        ArrayList<Integer> list=new ArrayList<>();
        /*找到每一层的个数,放到list集合中
        这里存放的是沙漏下半部分的图形个数,上半部分的个数把list集合倒过来就可以了
         */
        while(true){
    
    
            list.add(2*count+1);
            count++;
            sum+=2*count+1;
            if(2*sum-1>N){
    
    
                break;
            }
        }
        //按照题目给的输入样例,执行到这一行,list集合中的元素为1 3 5
        //count记录空格数
        count=0;
        //输出上半部分,把list集合倒过来变成5 3 1
        for(int i=list.size()-1;i>=0;i--){
    
    
            //输出空格
            for(int j=0;j<count;j++){
    
    
                System.out.print(" ");
            }
            //输出符号
            for(int k=0;k<list.get(i);k++){
    
    
                System.out.print(c);
            }
            count++;
            System.out.println();
        }
        //因为按照题目给的样例,执行到这一行count为3,所以要提前减成一个*前面的空格数
        count--;//注意这里要count--一下
        //输出下半部分,第一个*就不用输出了
        for(int i=1;i<list.size();i++){
    
    
            count--;
            for(int j=0;j<count;j++){
    
    
                System.out.print(" ");
            }
            for(int k=0;k<list.get(i);k++){
    
    
                System.out.print(c);
            }
            System.out.println();
        }
        //统计最后剩余没有使用的个数
        sum=0;//统计使用的符号的个数
        for(int i=0;i<list.size();i++){
    
    
            sum+=list.get(i);
        }
        sum=2*sum-1;
        System.out.print(N-sum);//N-sum就是未使用的
    }
}

For more topic analysis, follow the public account Algorithm Baby

Guess you like

Origin blog.csdn.net/CSDN_Lrcx/article/details/116168269