zoukankan      html  css  js  c++  java
  • 3.3——搜索 Many Formulas

    题目描述

    You are given a string S consisting of digits between 1 and 9, inclusive. You can insert the letter + into some of the positions (possibly none) between two letters in this string. Here, + must not occur consecutively after insertion.
    All strings that can be obtained in this way can be evaluated as formulas.
    Evaluate all possible formulas, and print the sum of the results.

    Constraints
    1≤|S|≤10
    All letters in S are digits between 1 and 9, inclusive.

    输入

    The input is given from Standard Input in the following format:
    S

    输出

    Print the sum of the evaluated value over all possible formulas.

    样例输入

    复制样例数据

    125
    

    样例输出

    176
    

    提示

    There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

    125
    1+25=26
    12+5=17
    1+2+5=8
    Thus, the sum is 125+26+17+8=176.

    来源/分类

    ABC045&ARC061 

    ps:有点不好理解。对递归理解应该加深

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    int a[15],x;
    ll ans;
    
    void dfs(int p,ll sum){//p表示位置
        if(p==x) {
            ans+=sum;
            //cout<<"ans="<<ans<<endl;
            return;
        }
        ll t=0;
        cout<<t<<endl;
        for(int i=p;i<x;i++){//枚举每个位置,在看下一个位置,不能取+号返回。根据递归,可以得到所有情况。
            t=t*10+a[i];
            //cout<<t<<endl;
            dfs(i+1,sum+t);
        }
    }
    int main()
    {
        ll s;
        scanf("%lld",&s);
        while(s){
            int b=s%10;
            a[x++]=b;
            s/=10;
        }
        reverse(a,a+x);
        dfs(0,0);
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    OPENSSL库使用--AES篇
    Linux inotify功能及实现原理
    LSI RAID
    Linux下关于热插拔硬盘的指令
    最长回文字串理解(学习Manacher's algorithm)
    pat 1068 动态规划/Fina More Conis
    (二)Myeclipse中关于jdk配置,解决版本不一致问题
    (一)MyEclipse配置Tomcat,与jsp程序运行
    pat 1047 解题心得
    pat 1038 Smallest Number解题心得
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319530.html
Copyright © 2011-2022 走看看