Print a rhombus - one of the most efficient algorithms

1 Title: Please write a program that can print graphics with any number of lines that meet the following rules, where the input is an int parameter, representing the number of graphics lines, and the number of lines is an odd number greater than 3.
insert image description here
2 Ideas for solving the problem:
Idea 1: Divide the graph into two parts, the upper part is a cycle, and the lower part is a cycle, and print " " and * respectively; but this method is too inefficient.
Idea 2: Divide the graph into two parts, and the upper part is used as a cycle to print " " and * respectively; and store the printed content of the previous (number of lines - 1/2) in the stack, and then print the content in the stack, This will be very efficient. When the input value is 101, the printing time is only 5ms, but when using idea 1, it takes 2851ms.
3 java code

import java.util.*;
/*
   用栈实现
 */
public class PrintChartOne {
    
    
    public static void main(String[] args){
    
    
        System.out.println("请输入字符串=? ");
        Scanner input=new Scanner(System.in);
        int lineNumber=input.nextInt();
        long startTime = System.currentTimeMillis();//获取当前时间
        PrintChartOne printChartOne=new PrintChartOne();
        printChartOne.printPentagram(lineNumber);
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
    }
    public  void printPentagram(int lineNumber){
    
    
        if ((lineNumber>3) &&(lineNumber%2==1) ) {
    
    
            String sStar = "*";
            String sBlank = " ";//空格
            //字符串栈
            Stack sPrintStackS = new Stack();
            for (int i = 1; i <= (lineNumber - 1) / 2 + 1; i++) {
    
    
                String sPrintStar = "";//用来打印每行的"*"
                String sPrintBlank = "";//用来打印每行的" "
                //前i 每行*的数量
                int sStarNumber = 2 * i - 1;
                for (int j = 1; j <= sStarNumber; j++) {
    
    
                    sPrintStar = sPrintStar + sStar;
                }
                //前i 每行" "的数量
                int sLeftBlankNumber = (lineNumber - sStarNumber) / 2;
                for (int k = 1; k <= sLeftBlankNumber; k++) {
    
    
                    sPrintBlank = sPrintBlank + sBlank;
                }
                //打印前(lineNumber-1)/2+1行
                String sPrint = sPrintBlank + sPrintStar;
                System.out.println(sPrint);
                //前i-1行内容写入栈
                if (i <= (lineNumber - 1) / 2) {
    
    
                    sPrintStackS.push(sPrint);
                }
            }
            //打印后(lineNumber-1)/2行
            while (!sPrintStackS.empty()) {
    
    
                System.out.println(sPrintStackS.pop());
            }
        } else System.out.println("请输入大于3的奇数!");
    }
}



4 running results
insert image description here

Guess you like

Origin blog.csdn.net/helloworldchina/article/details/107380442