• 求1到9中插入“+”或“-”使结果为100的解


    ---------------------------------------------------------------------------

    细节:数字划分与插入运算符逻辑分开

    #include<iostream>
    #include<vector>
    #include<string>
    #define maxn 15
    using namespace std;

    vector<string>res;
    vector<int>tmp;
    int a[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
    void judge(int target,int index,vector<int>&res,string &s) {
    if (index == res.size()) {
    if (target == 100) {
    s = s + "=100";
    printf("%s ", s.c_str());
    }

    return ;
    }
    int sm = res[index];
    //res.pop_back();
    for (int i = 0; i < 2; i++) {
    if (i == 0) {
    string ss = s + "-" + to_string(sm);
    judge(target -sm, index + 1, res,ss);
    }
    else {
    string ss = s + "+" + to_string(sm);
    judge(target + sm,index+1, res, ss);
    }
    }

    }
    void solve( int index) {
    if (index == maxn+1) {
    string s = to_string(tmp[0]);
    judge(tmp[0],1,tmp,s);
    return;
    }
    int sum = 0;
    for (int i = index; i <= maxn; i++) {
    sum = sum * 10 + a[i];
    //s = s + to_string(sum);
    tmp.push_back(sum);
    //string ss = s + to_string(sum);
    solve(i+1);
    tmp.pop_back();
    }
    }
    int main() {
    string ss = "";
    solve(1);
    return 0;
    }

  • 相关阅读:
    使用外部 toolchain 编译 openwrt
    openwrt network 初始化
    attribute constructor&destructor
    ditaa
    quilt
    转载
    无线网络的一些基础概念
    FIR300M刷openwrt
    翻译:A Tutorial on the Device Tree (Zynq) -- Part V
    翻译:A Tutorial on the Device Tree (Zynq) -- Part IV
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/11819542.html
Copyright © 2020-2023  润新知