Many Formulas

这是一个编程问题,名为C: Many Formulas,要求在1秒内完成,内存限制为128 MB。目前已有145次提交,80次解决。详细内容包括题目描述、输入格式、输出要求、样例输入和输出以及相关提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值