UPC-6467 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.
题意:给出一段数字,在数字中随意加加号,加的加号不能相邻,问所有加加号的情况求和之后是多少。
深搜暴力即可。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
char str[15];
LL ans;
void dfs(LL sum,int now,int len)
{
    if(now==len)
    {
//        printf("====%lld
",sum);
        ans+=sum;
        return;
    }
    for(int i=now; i<len; i++)
    {
        LL tmp=0;
        for(int j=now; j<=i; j++)
            tmp=tmp*10+(str[j]-'0');
//        printf("now=%d  tmp=%lld   sum=%lld
",now,tmp,sum);
        dfs(sum+tmp,i+1,len);
    }
    return;
}
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        ans=0;
        int len=strlen(str);
        dfs(0,0,len);
        printf("%lld
",ans);
    }
}
原文地址:https://www.cnblogs.com/kuronekonano/p/11135758.html