【递归练习】算24点

代码


#include <cstdio>
#include <cmath>
#define EPS 1e-8
using namespace std;

double a[4];

bool judge(double a[], int n)
{
    if (n == 1){
        if (fabs(a[0]-24) <= EPS) return 1;
        else return 0;
    }
    double b[4];
    for (int i = 0; i < n-1; ++i)
        for (int j = i+1; j < n; ++j){
            int rem = 0;
            for (int k = 0; k < n; ++k)
                if (k != i && k != j) b[rem++] = a[k];
            b[rem] = a[i] + a[j];
            if (judge(b, rem+1)) return 1;
            b[rem] = a[i] - a[j];
            if (judge(b, rem+1)) return 1;
            b[rem] = a[j] - a[i];
            if (judge(b, rem+1)) return 1;
            b[rem] = a[i] * a[j];
            if (judge(b, rem+1)) return 1;
            if (fabs(a[j]) > EPS){
                b[rem] = a[i] / a[j];
                if (judge(b, rem+1)) return 1;
            }
            if (fabs(a[i]) > EPS){
                b[rem] = a[j] / a[i];
                if (judge(b, rem+1)) return 1;
            }
        }
    return 0;
}

int main()
{
    while(~scanf("%lf%lf%lf%lf", &a[0], &a[1], &a[2], &a[3]) &&
          (a[0] || a[1] || a[2] || a[3]))
            judge(a, 4) ? printf("YES\n"): printf("NO\n");
    return 0;
}

说明


  • 输入四个数,输出是否能算出24
  • 允许出现分数/负数
  • 比较浮点数是否相等不能用==
### 递归练习题及代码示例 #### 练习题一:计阶乘 计一个正整数 `n` 的阶乘(即 `n! = n * (n-1) * ... * 1`)。 **代码示例:** ```java public class Factorial { public static int factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n - 1); } public static void main(String[] args) { System.out.println(factorial(5)); // 输出 120 } } ``` 此代码通过递归方式实现阶乘的计[^1]。 --- #### 练习题二:斐波那契数列 计斐波那契数列的第 `n` 项(即 `F(n) = F(n-1) + F(n-2)`,其中 `F(0) = 0, F(1) = 1`)。 **代码示例:** ```java public class Fibonacci { public static int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } return fibonacci(n - 1) + fibonacci(n - 2); } public static void main(String[] args) { System.out.println(fibonacci(10)); // 输出 55 } } ``` 该代码展示了如何通过递归斐波那契数列的值[^4]。 --- #### 练习题三:爬楼梯问题 假设你正在爬楼梯。需要 `n` 阶你才能到达楼顶。每次你可以爬 `1` 或 `2` 阶。问有多少种不同的方法可以爬到楼顶? **代码示例:** ```javascript var climbStairs = function(n) { if (n === 0 || n === 1) { return 1; } return climbStairs(n - 1) + climbStairs(n - 2); }; console.log(climbStairs(3)); // 输出 3 ``` 这段代码使用递归解决爬楼梯问题,类似于斐波那契数列的计逻辑。 --- #### 练习题四:分解正整数为若干正整数的乘积 给定一个正整数 `a`,将其分解为若干个正整数的乘积,并满足 `1 < a1 <= a2 <= a3 <= ... <= an`,问这样的分解有多少种可能? **代码示例:** ```python def count_decompositions(a): def helper(current, start): if current == 1: return 1 count = 0 for i in range(start, a + 1): if current % i == 0: count += helper(current // i, i) return count return helper(a, 2) print(count_decompositions(20)) # 输出 4 ``` 上述代码通过递归实现了对正整数分解的计数问题[^5]。 --- #### 练习题五:二叉树的最大深度 给定一棵二叉树,求其最大深度。 **代码示例:** ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class BinaryTreeMaxDepth { public static int maxDepth(TreeNode root) { if (root == null) { return 0; } int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return Math.max(leftDepth, rightDepth) + 1; } public static void main(String[] args) { TreeNode tree = new TreeNode(1); tree.left = new TreeNode(2); tree.right = new TreeNode(3); tree.left.left = new TreeNode(4); tree.left.right = new TreeNode(5); System.out.println(maxDepth(tree)); // 输出 3 } } ``` 此代码通过递归二叉树的最大深度[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值