回溯+剪枝:求组合总数,记给别人做的一个小应用。

前些天我的亲戚找到我,说找我给他做一个简单支票计算脚本,要求如下:
输入不同数量的支票,每张支票只能用一次,给定一个目标数,求出用已给支票组合出目标数的所有组合方式,如果无解,给出可解的低于该目标数的最大数字的所有解。
例如:
回溯+剪枝:求组合总数,记给别人做的一个小应用。_第1张图片
也就是求数字的所有组合问题。
思路是回溯和剪枝。
算法如下:

class Solution {
private:
    vector candidates, path;
    vector > ans;
public:
    bool dfs(int start, int target) {
        if (target == 0) {
            ans.push_back(path);
            return 1;
        }
        for (int i = start; i < candidates.size() && target - candidates[i] >= 0; ++i)
        {
            if (i > start&& candidates[i] == candidates[i - 1])//剪枝
                continue;
            path.push_back(candidates[i]);
            dfs(i + 1, target - candidates[i]);
            path.pop_back();
        }
        return 0;
    }

    vector > combinationSum2(vector& candidates, int target) {
        ans={};
        sort(candidates.begin(), candidates.end());
        this->candidates = candidates;
        dfs(0, target);
        return ans;
    }
};

求解时记得每次要初始化ans数组,不然会出现赘余导致问题。
完整代码如下:

#include 
#include 
#include 
using namespace std;
class Solution {
private:
    vector candidates, path;
    vector > ans;
public:
    bool dfs(int start, int target) {
        if (target == 0) {
            ans.push_back(path);
            return 1;
        }
        for (int i = start; i < candidates.size() && target - candidates[i] >= 0; ++i)
        {
            if (i > start&& candidates[i] == candidates[i - 1])
                continue;
            path.push_back(candidates[i]);
            dfs(i + 1, target - candidates[i]);
            path.pop_back();
        }
        return 0;
    }

    vector > combinationSum2(vector& candidates, int target) {
        ans={};
        sort(candidates.begin(), candidates.end());
        this->candidates = candidates;
        dfs(0, target);
        return ans;
    }
};
int main()
{
    cout<开始处理第"<> c_num;
        Solution solution = Solution();
        cout << "请依次输入每张支票的面额:" << endl;
        vector candidates;
        for (int i = 0; i < c_num; ++i) {
            int ci = 0; cin >> ci;
            candidates.push_back(ci);
        }
        cout << "请输入目标数字的个数:" << endl;
        int num_ans=0;
        cin>>num_ans;
        while(num_ans>0){
        cout << "请输入目标数字:" << endl;
        int target = 0; cin >> target;
        vector > res= solution.combinationSum2(candidates, target);
        if (res.size() != 0) {
            cout << "目标数字:" << target << "的所有解法为:" << endl;
            for (int i = 0; i < res.size(); ++i) {
                for (int j = 0; j < res[i].size(); ++j) {
                    if (j < res[i].size() - 1) cout << res[i][j] << " + ";
                    else cout << res[i][j] << " = " << target;
                }
                cout << "" << endl;
            }            cout << endl;
        }
        else {
            if (target > candidates.back()) {
                int target_down = target - 1; Solution solution_down = Solution();
                vector > res_down= solution_down.combinationSum2(candidates, target_down);
                while (res_down.size() == 0) {
                    target_down--; res_down = solution_down.combinationSum2(candidates, target_down);
                }
                cout << "目标数字无解!低于且最接近目标数的可解整数 " << target_down << " 的所有解法为:" << endl;
                for (int i = 0; i < res_down.size(); ++i) {
                    for (int j = 0; j < res_down[i].size(); ++j) {
                        if (j < res_down[i].size() - 1) cout << res_down[i][j] << " + ";
                        else cout << res_down[i][j] << " = " << target_down;
                    }
                    cout << "" << endl;
                }
                                int target_up = target + 1; Solution solution_up = Solution();
                /*vector > res_up = solution_up.combinationSum2(candidates, target_up);
                while (res_up.size() == 0&&target_up第"<

你可能感兴趣的:(c++,算法)