32.二分数字组合(回溯+dfs)【C++已解决】

#include <iostream>
#include <vector>

//回溯暴力搜索
void backTracking(int& ans, int i, int j, int k, const int& n, const int& A, const int& B, const std::vector<int>& array_a){
    if(i >= n){
        if(j % 10 == A && k % 10 == B){
            ans++;
        }else if(j == 0 && k % 10 == B || k == 0 && j % 10 == A){
            ans++;
        }
        return;
    }
    backTracking(ans, i + 1, j + array_a[i], k, n, A, B,array_a);
    backTracking(ans, i + 1, j , k+ array_a[i], n, A, B,array_a);
}
int solution(int n, int A, int B, std::vector<int> array_a) {
    // Please write your code here
    int ans = 0;
    backTracking(ans, 0, 0, 0, n, A, B, array_a);
    return ans;
}

int main() {
    //  You can add more test cases here
    std::vector<int> array1 = {1, 1, 1};
    std::vector<int> array2 = {1, 1, 1};
    std::vector<int> array3 = {1, 1};

    std::cout << (solution(3, 1, 2, array1) == 3) << std::endl;
    std::cout << (solution(3, 3, 5, array2) == 1) << std::endl;
    std::cout << (solution(2, 1, 1, array3) == 2) << std::endl;

    return 0;
}

两种情况选或者不选

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值