题目:求0—7所能组成的奇数个数。
思路: 0-7不能重复 统计1位,2位,3位, 4位, 5位, 6位,7位,8位,每个位数的奇数个数 个数 4 6*4 6*7*4 6*7*7*4
* 6*7*7*7*4
public class 第四十三题计算奇数个数 { public static void main(String[] args) { /* * 思路: 0-7不能重复 统计1位,2位,3位, 4位, 5位, 6位,7位,8位,每个位数的奇数个数 个数 4 6*4 6*7*4 6*7*7*4 * 6*7*7*7*4 */
// 统计总数
int total = 0; // 8次循环
for (int i = 0; i < 8; i++) { total += getValue(i); } System.out.println("总数为:" + total + "个"); } //递归获取第n项的值
public static int getValue(int n) { int a0 = 4; int a1 = 24; if (n == 0) { return a0; } else if (n == 1) { return a1; } else { return getValue(n - 1) * 7; } } }