Last active
December 20, 2015 22:49
-
-
Save charlespunk/6208157 to your computer and use it in GitHub Desktop.
序列生成分析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 给定一个表达式2^i*2^j,其中i,j为非负整数。请找到一种方法,生成如下序列: | |
* 2^0 * 5^0 = 1 | |
* 2^1 * 5^0 = 2 | |
* 2^2 * 5^0 = 4 | |
* 2^0 * 5^1 = 5 | |
* 2^3 * 5^0 = 8 | |
* 2^1 * 5^1 = 10 | |
* 2^4 * 5^0 = 16 | |
* 2^2 * 5^1 = 20 | |
* 2^0 * 5^2 = 25 | |
* ... | |
* ... | |
* ... | |
*/ | |
public static void main(String[] args) { | |
int n = 10; | |
int[] out = new int[n]; | |
int pos = 0; | |
out[pos++] = 1; | |
int i2 = 0, i5 = 0; | |
while(n-- > 1){ | |
int m2 = out[i2] * 2; | |
int m5 = out[i5] * 5; | |
if(m2 < m5){ | |
out[pos++] = m2; | |
i2++; | |
} | |
else if(m2 > m5){ | |
out[pos++] = m5; | |
i5++; | |
} | |
else{ | |
out[pos++] = m2; | |
i2++; | |
i5++; | |
} | |
} | |
System.out.println(Arrays.toString(out)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment