【Java】蓝桥杯PREV - 2 打印十字图

一、题目描述(省略)

输入层数 n, 输出如下十字图(官网给的图实在太丑唉)

二、代码

import java.util.Scanner;



public class Main {
	
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int col = (2 * n + 2) * 2 + 1;
		String[][] map = new String[col][col];
		for(int i = 0; i < col; i ++){
			for(int j = 0; j < col; j++)
				map[i][j] = ".";
		}
		
//		int[][] dir = { 
//				{1, 0},
//				{0, 1},
//				{-1, 0},
//				{0, -1}		};
		
		int mid = col / 2;
		map[mid][mid] = "$";
		for(int i = 1; i <= 2 ; i++){
			map[mid][mid + i] = "$";
			map[mid + i][mid] = "$";
			map[mid - i][mid] = "$";
			map[mid][mid - i] = "$";
		}
		
		int longlong = 5;
		int x1 = mid, x2 = mid, x3 = mid, x4 = mid;
		int y1 = mid, y2 = mid, y3 = mid, y4 = mid;
		for(int i = 1; i <= n; i++){
			
			if(i > 1){
				longlong += 4;
			}
			
			int shortshort = 2;
			
			x1 -= 2;
			y1 -= 2;
			map[y1][x1] = "$";
			
			x2 += 2;
			y2 -= 2;
			map[y2][x2] = "$";
			
			x3 += 2;
			y3 += 2;
			map[y3][x3] = "$";
			
			x4 -= 2;
			y4 += 2;
			map[y4][x4] = "$";
			
			for(int j = 0; j < longlong; j++){
				 map[y1 + j][x1 - 2] = "$";
				 map[y1 - 2][x1 + j] = "$";
				 map[y2 + j][x2 + 2] = "$";
				 map[y4 + 2][x4 + j] = "$";
			}
			
			for(int j = 0; j < shortshort; j++){
				map[y1][x1 - j] = "$";
				map[y1 - j][x1] = "$";
				
				map[y2 - j][x2] = "$";
				map[y2][x2 + j] = "$";
				
				map[y3 + j][x3] = "$";
				map[y3][x3 + j] = "$";
				
				map[y4 + j][x4] = "$";
				map[y4][x4 - j] = "$";
			}
			
			
			
		}
		
		
		for(int i = 0; i < col; i++){
			for(int j = 0; j < col; j++)
				System.out.print(map[i][j]);
			System.out.println();
		}
		
	
	
	
	}
	
	
	
	

}

三、思路记录

  1. 观察规律,求出 n 层的矩阵的行数(也是列数);然后从中间点出来,一层层往外渲染。
  2. 代码写得不是很好,因为想的时候用的是坐标系的方式,变量 x, y 的意义都是坐标系中 x, y 轴坐标,但实现使用的是二维数组,“行、列” 实则是 “ y, x ”;但懒得改了【真的是太懒了唉】

四、结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值