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\n",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\n",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\n",ans);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值