Many Formulas

问题 C: Many Formulas

时间限制: 1 Sec   内存限制: 128 MB
提交: 145   解决: 80
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const long long INF=0x7FFFFFFFFFFFFFFFLL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
char s[15];
 
int main()
{
    int i,j,len,x;
    ll temp,sum=0;
    scanf("%s",s);
    len=strlen(s);
    x=1<<(len-1);
    for(i=0;i<x;i++)
    {
        temp=s[0]-'0';
        for(j=0;j<len;j++)
        {
            if(j==len-1 || i&(1<<j))
            {
                sum=sum+temp;
                temp=0;
                if(j==len-1)
                    break;
            }
            temp=temp*10+(s[j+1]-'0');
        }
    }
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ToBeYours/article/details/80496875
今日推荐