zoukankan      html  css  js  c++  java
  • 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;
    }
    

    大佬的博客

  • 相关阅读:
    小程序实现无限瀑布流
    Vue H5 项目模板
    Taro使用mobx做国际化小程序
    一次国际化记录以及平铺JSON数据
    Promise(interesting)
    返回状态码
    CSS属性兼容写法
    在DOM加载之前insertScript
    关于吸烟
    前端优化措施
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301592.html
Copyright © 2011-2022 走看看