`
mysh
  • 浏览: 28949 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Project Euler p14 - Longest Collatz sequence

阅读更多

发现一个好玩的站, 没事刷刷题

http://projecteuler.net/problem=14

 

找一百万以内的最大起始数, 一个个数过去就好了, 用上DP, 45ms

 

	@Test
	public void t2() {
		final int R = 1_000_001;
		int[] record = new int[R];
		record[1] = 1;
		long ti;
		int c, maxCount = 0, maxR = 0;
		for (int i = 2; i < R; i++) {
			c = 0;
			ti = i;
			while (ti > i || record[((int) ti)] == 0) {
				if (ti % 2 == 0) ti /= 2;
				else ti = ti * 3 + 1;
				c++;
			}
			c += record[((int) ti)];
			record[i] = c;

			if (c > maxCount) {
				maxCount = c;
				maxR = i;
			}
		}
		System.out.println(maxR);
	}

 

1
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics