Java面试_打印矩阵

Java面试_打印矩阵

按照要求填充矩阵

按照上面图的要求打印矩阵,看一下要求,其实就是按照一定的规律遍历

一个二维数组,首先向右走一个单位,然后向左下,直到边界,然后向下,

右上知道边界。向右越界了就向下,向下越界了就向右,知道到达右下角。

翻译成代码逻辑

package swing;

public class JuZhen {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int n = 4;
		// 二维数组填充器 返回填充的二维数组下标和填充的数字
		IndexGen g = new IndexGen(n);
		int[][] nums = new int[n][n];
		while (g.haveNext()) {
			IndexInfo index = g.next();
			nums[index.d1][index.d2] = index.num;
		}
		// 打印内容
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int pnum = nums[i][j];
				String pnums = String.valueOf(pnum);
				if (pnums.length() == 1)
					pnums = " " + pnums;
				System.out.print(pnums + " ");
			}
			System.out.println();
		}

	}

	private static class IndexGen {
		private IndexInfo currentIndex;
		private int maxIndex;
		private int count = 1;
		private int totalNum;
		private int flag = 1;

		public IndexGen(int n) {
			this.maxIndex = n - 1;
			this.totalNum = n * n;
		}

		private boolean haveNext() {
			if (count <= totalNum)
				return true;
			else
				return false;
		}

		public IndexInfo next() {
			// 起始状态
			if (count == 1) {
				currentIndex = new IndexInfo(0, 0);
				currentIndex.num = count;
				count++;
				return currentIndex;
			}

			if (flag == 1) {
				// 如果第二个下标达到最大值 向下 否则向右
				if (currentIndex.isIndex2Max(maxIndex)) {
					currentIndex = currentIndex.down();
				} else
					currentIndex = currentIndex.rigth();
				flag++;
			} else if (flag == 2) {
				// 斜下 直到第一个下标达到最大值 或者 第二个下标达到最小值
				currentIndex = currentIndex.xieXia();
				if (currentIndex.isIndex2Min(maxIndex)
						|| currentIndex.isIndex1Max(maxIndex))
					flag++;
			} else if (flag == 3) {
				// 如果第一个下标达到最大值 向右 否则向下
				if (currentIndex.isIndex1Max(maxIndex)) {
					currentIndex = currentIndex.rigth();
				} else
					currentIndex = currentIndex.down();

				flag++;
			} else if (flag == 4) {
				// 斜上 下一个状态是1
				currentIndex = currentIndex.xieShang();
				if (currentIndex.isIndex1Min(maxIndex)
						|| currentIndex.isIndex2Max(maxIndex))
					flag = 1;

			}
			currentIndex.num = count;
			count++;

			return currentIndex;
		}

	}

	private static class IndexInfo {
		private int d1;

		private int num;

		private int d2;

		public IndexInfo(int d1, int d2) {
			this.d1 = d1;
			this.d2 = d2;
		}

		public IndexInfo getNew(int d1Num, int d2Num) {
			return new IndexInfo(this.d1 + d1Num, this.d2 + d2Num);
		}

		public IndexInfo rigth() {
			return getNew(0, 1);
		}

		public IndexInfo xieXia() {
			return getNew(1, -1);
		}

		public IndexInfo down() {
			return getNew(1, 0);
		}

		public IndexInfo xieShang() {
			return getNew(-1, 1);
		}

		public boolean isIndex1Max(int n) {
			if (d1 == n)
				return true;
			else
				return false;
		}

		public boolean isIndex2Max(int n) {
			if (d2 == n)
				return true;
			else
				return false;
		}

		public boolean isIndex1Min(int n) {
			if (d1 == 0)
				return true;
			else
				return false;
		}

		public boolean isIndex2Min(int n) {
			if (d2 == 0)
				return true;
			else
				return false;
		}

	}

}

这么写if else 太多 可以用状态模式改进

package swing;

public class JuZhen2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int n = 4;
		IndexGen g = new IndexGen(n - 1);
		int[][] nums = new int[n][n];
		while (g.haveNext()) {
			IndexInfo index = g.next();
			nums[index.d1][index.d2] = index.num;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int pnum = nums[i][j];
				String pnums = String.valueOf(pnum);
				if (pnums.length() == 1)
					pnums = " " + pnums;
				System.out.print(pnums + " ");
			}
			System.out.println();
		}

	}

	private static class IndexInfo {
		private int d1;
		private int num;
		private int maxIndex;

		private int d2;

		public IndexInfo(int d1, int d2, int maxIndex) {
			if (d1 < 0 || d1 > maxIndex)
				throw new IllegalArgumentException(String.valueOf(d1));
			if (d2 < 0 || d2 > maxIndex)
				throw new IllegalArgumentException(String.valueOf(d2));
			this.d1 = d1;
			this.d2 = d2;
			this.maxIndex = maxIndex;
		}

		public IndexInfo getNew(int d1, int d2) {
			return new IndexInfo(this.d1 + d1, this.d2 + d2, this.maxIndex);
		}

		public IndexInfo rigth() {
			return getNew(0, 1);
		}

		public IndexInfo xieXia() {
			return getNew(1, -1);
		}

		public IndexInfo down() {
			return getNew(1, 0);
		}

		public IndexInfo xieShang() {
			return getNew(-1, 1);
		}

		public boolean isIndex1Max() {
			if (d1 == maxIndex)
				return true;
			else
				return false;
		}

		public boolean isIndex1Min() {
			if (d1 == 0)
				return true;
			else
				return false;
		}

		public boolean isIndex2Min() {
			if (d2 == 0)
				return true;
			else
				return false;
		}

		public boolean isIndex2Max() {
			if (d2 == maxIndex)
				return true;
			else
				return false;
		}

	}

	private static final IIndexState RIGHT_STATE = new RigthState();
	private static final IIndexState XIESHANG_STATE = new XieShangState();
	private static final IIndexState XIEXIA_STATE = new XieXiaState();
	private static final IIndexState DOWN_STATE = new DownState();
	private static final IIndexState END_STATE = new EndState();

	private static interface IIndexState {

		IndexInfo nextIndexID(IndexInfo indexID);

		IIndexState getNextIndexState(IndexInfo indexID);

	}

	private static abstract class AbstractState implements IIndexState {

	}

	/**
	 * 开始状态
	 * 
	 */
	private static class StartState extends AbstractState {

		private int n;

		public StartState(int n) {
			this.n = n;
		}

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return new IndexInfo(0, 0, n);
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			return RIGHT_STATE;
		}
	}

	/**
	 * 结束状态
	 * 
	 */
	private static class EndState extends AbstractState {

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return null;
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			return null;
		}
	}

	/**
	 * 向右状态
	 * 
	 */
	private static class RigthState extends AbstractState {

		public RigthState() {
		}

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return indexID.rigth();
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			if (indexID.isIndex1Max() && indexID.isIndex2Max())
				return END_STATE;
			if (indexID.isIndex1Max())
				return XIESHANG_STATE;
			else
				return XIEXIA_STATE;
		}
	}

	/**
	 * 斜下状态
	 * 
	 */
	private static class XieXiaState extends AbstractState {

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return indexID.xieXia();
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			if (indexID.isIndex1Max()) {
				return RIGHT_STATE;
			} else if (indexID.isIndex2Min()) {
				return DOWN_STATE;
			} else
				return this;
		}

	}

	/**
	 * 斜上
	 * 
	 */
	private static class XieShangState extends AbstractState {

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return indexID.xieShang();
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			if (indexID.isIndex2Max()) {
				return DOWN_STATE;
			} else if (indexID.isIndex1Min()) {
				return RIGHT_STATE;
			} else {
				return this;
			}

		}
	}

	/**
	 * 向下
	 * 
	 */
	private static class DownState extends AbstractState {

		@Override
		public IndexInfo nextIndexID(IndexInfo indexID) {
			return indexID.down();
		}

		@Override
		public IIndexState getNextIndexState(IndexInfo indexID) {
			if (indexID.isIndex2Min())
				return XIESHANG_STATE;
			else
				return XIEXIA_STATE;

		}

	}

	private static class IndexGen {
		private IndexInfo currentIndex;
		private int count = 1;
		private IIndexState IIndexState;

		public IndexGen(int n) {
			this.IIndexState = new StartState(n);
		}

		private boolean haveNext() {
			if (this.IIndexState != END_STATE)
				return true;
			else
				return false;
		}

		public IndexInfo next() {
			currentIndex = this.IIndexState.nextIndexID(currentIndex);
			this.IIndexState = this.IIndexState.getNextIndexState(currentIndex);
			currentIndex.num = count;
			count++;
			return currentIndex;
		}

	}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值