[Daily Blue Bridge] 8. The real question of "Printing the Cross Chart" in the Java Group of the 13th Provincial Games

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Print cross chart

Xiao Ming designed a cross-shaped logo for an organization (not the Red Cross), as shown below:

The other party also needs to output the logo in the form of characters in the computer dos window, and can control the number of layers arbitrarily,

In order to accurately compare the number of blanks, the program requires a period (.) to replace the blanks in the line

 

Input format: a positive integer n (n<30) indicates the number of layers of graphics to be printed

Output:

The sign corresponding to the number of surrounding layers

Such as: user input:

1

The program should output:

 

Another example: user input

3

The program should output:

 

Please observe the sample carefully, paying particular attention to the number of periods and the output position

Resource agreement:
peak memory consumption <64M

CPU consumption <1000ms

Please output in strict accordance with the requirements, and do not print superfluous content similar to: "Please enter...".

All the codes are placed in the same source file. After the test passes, copy and submit the source code.

Note: the main function needs to return 0

Note: Only use ANSI C/ANSI C++ standards, do not call special functions that depend on the compilation environment or operating system

Note: All dependent functions must be clearly in the source file, #include<xxx> can not be set through the project to ignore common header files

When submitting, pay attention to selecting the desired compiler type

Problem-solving ideas:

The focus of this question is to carefully observe the characteristics of each layer of graphics, and then infer the laws of each line.

The idea in this question is: first find the rules for printing each line, and then print each layer of graphics separately, and then use a two-dimensional array to record the position of the "$" sign in each layer in two dimensions In the array, the last two-dimensional array stores the data of the entire graph, and then the two-dimensional array can be output.

Answer source code:

package 一三年省赛真题;

import java.util.Scanner;

public class Year2013_t8 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		drawAll(m);
	}
	
	/**
	 * 绘制整个图形
	 * @param m 图形共有多少层
	 * */
	public static void drawAll(int m) {
		int row = 5+4*m;
		int list = 5+4*m;
		String[][] drawArr = new String[row][list];		//建立存放该图形的二维数组
		for (int i = 0; i <= m; i++) {
			drawN(m, i, drawArr);
		}
		
		//将图形输出
		for (int i = 0; i < drawArr.length; i++) {
			for (int j = 0; j < drawArr.length; j++) {
				if (drawArr[i][j]!="$") {
					drawArr[i][j] = ".";
				}
				System.out.print(drawArr[i][j]);
			}
			System.out.println();
		}
	}
	
	
	/**
	 * 画第n层图形
	 * @param m 图形的总层数
	 * @param n 当前绘制的层数
	 * @param drawArr 存放图形的二维数组
	 * */
	public static void drawN(int m,int n,String[][] drawArr) {
		
		int r = 2*(m-n);		//确定该层图形的起始横纵坐标
		int width = 5 + 4*n;	//获取该层图形的最大宽度
		int height = 5 + 4*n;			//获取该层图形的最大高度
		
		//绘制第一行和倒数第一行
		for (int i = 2; i < height-2; i++) {
			drawArr[r+0][r+i] = "$";
			drawArr[r+height-1][r+i] = "$";
		}
		
		//绘制第二行和倒数第二行
		drawArr[r+1][r+2] = "$";
		drawArr[r+1][r+width-3] = "$";
		drawArr[r+height-2][r+2] = "$";
		drawArr[r+height-2][r+width-3] = "$";
		
		//绘制第三行和倒数第三行
		for (int i = 0; i < 3; i++) {
			drawArr[r+2][r+i] = "$";
			drawArr[r+2][r+width-1-i] = "$";
			drawArr[r+height-3][r+i] = "$";
			drawArr[r+height-3][r+width-1-i] = "$";
		}
		
		//绘制第四到倒数第四行
		for (int i = 3; i < height-3; i++) {
			drawArr[r+i][r+0] = "$";
			drawArr[r+i][r+width-1] = "$";
		}
		
	}
	
}

 

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112757277