Java Blue Bridge Cup 2014 Second Question

Java print cross chart

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

The other party also needs to output the sign 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 that the blanks in the line be replaced by periods (.), and the red parts are replaced by $.

public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		fun(n);
	}
 
	public static void fun(int n) {
    
    
		int t = n * 4 + 5;
		char arr[][] = new char[t][t];
		// 初始化二维数组为.
		for (int i = 0; i < arr.length; i++) {
    
    
			for (int j = 0; j < arr.length; j++) {
    
    
				arr[i][j] = '.';
			}
		}
 
		for (int i = 0; i < t / 2; i += 2) {
    
    
			for (int j = i + 2; j < t / 2 + 1; j++) {
    
    
				arr[i][j] = '$';
				arr[j][i] = '$';
			}
			arr[i + 1][i + 2] = '$';
			arr[i + 2][i + 1] = '$';
			arr[i + 2][i + 2] = '$';
		}
 
		for (int x = 0; x < t / 2 + 1; x++) {
    
    
			for (int i = 0, j = t - 1; j >= i; i++, j--) {
    
    
				arr[x][j] = arr[x][i];
			}
		}
		for (int x = 0; x < t; x++) {
    
    
			for (int i = 0, j = t - 1; j >= i; i++, j--) {
    
    
				arr[j][x] = arr[i][x];
			}
		}
 
		for (int i = 0; i < arr.length; i++) {
    
    
			for (int j = 0; j < arr.length; j++) {
    
    
				System.out.print(arr[i][j]);
			}
			System.out.println();
		}
	}
}

insert image description here
First analyze the upper left part, copy the left and right mirrors, and copy the upper and lower mirrors.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325097340&siteId=291194637
Recommended