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.
样例输入 Copy
125
样例输出 Copy
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.

#include <bits/stdc++.h>
using namespace std;
string s;
int cnt;
long long sum,t;
int main() {
    //freopen("in", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    cnt = s.size();
    for(int i = 0; i < (1 << (cnt - 1));i++){
        t = s[0] - '0';
        for(int j = 0; j < cnt; j++){
            if(i & (1  << j) || j == cnt - 1){
                sum += t;
                t = 0;
                if(j == cnt - 1) break;
            }

            t = t * 10 + (s[j + 1] - '0');

        }
    }
    cout << sum << endl;
    return 0;
}

大佬的博客

原文地址:https://www.cnblogs.com/xcfxcf/p/12301592.html