`
unbounder
  • 浏览: 174751 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

复习笔记3

    博客分类:
  • job
阅读更多
摩托罗拉笔试
螺旋矩阵输出
递归思路
public int[][] create(int begin, int num) {
		int[][] temp = new int[num][num];
		if (num == 1) {
			temp[0][0] = begin;
		} else if (num == 2) {
			temp[0][0] = begin;
			temp[0][1] = begin + 1;
			temp[1][0] = begin + 3;
			temp[1][1] = begin + 2;
		} else {
			int[][] temp1 = create(begin + 4 * num - 4, num - 2);
			for (int i = 0; i < num; i++) {
				temp[0][i] = begin + i;
			}
			for (int i = 1; i < num - 1; i++) {
				for (int j = 0; j < num; j++) {
					if (j == 0) {
						temp[i][j] = 4 * num - 4 + begin - i;
					} else if (j == num - 1) {
						temp[i][j] = begin + num - 1 + i;
					} else {
						temp[i][j] = temp1[i - 1][j - 1];
					}
				}
			}
			for (int i = 0; i < num; i++) {
				temp[num - 1][i] = 3 * num - 3 + begin - i;
			}
		}

		return temp;
	}


百度
洗牌
要求严格和原来位置不一样
Card[] array = new Card[52];

	private void newPork() {
		Random r = new Random();
		for (int j = 0; j < 52; j++) {
			Card c = array[j];
			if (c.isRight()) {
				continue;
			} else {
				int i = r.nextInt(51);
				if (i != c.site) {//这个很烦,用while()会比较好,
					Card temp = c;
					array[j] = array[i];
					array[j].setNewsite(j);
					temp.setNewsite(i);
					array[i] = temp;
				}
			}
		}

	}



card类
class Card {
	int value;
	int type;
	int old = -1;
	int site = -1;

	public Card(int value, int type) {
		this.value = value;
		this.type = type;
	}

	public boolean isRight() {
		if (old == site) {
			return false;
		} else {
			return true;
		}
	}

	public void setOld(int old) {
		this.old = old;
	}

	public void setNewsite(int _newsite) {
		this.site = _newsite;

	}
}

初始化略
不考虑随机函数的时间复杂度,大概为O[n]
完备性证明用反证
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics