题目:求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;
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2021-06-02
  • 2021-08-26
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案