zoukankan      html  css  js  c++  java
  • D

    Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.

    Input
    Each case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.

    Output
    The output contains one line for each data set : the number of ways you can find to make the equation.

    Sample Input
    123456789 3
    21 1

    Sample Output
    18
    1
    把这道题贴出来的意思就是很玄学,我的代码连样例都没有过竟然AC了,我也是服,而且比网上的标程还要快
    code

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    const int maxn = 100;
    
    char str[maxn];
    ll val,len,ans;
    
    void dfs(ll sum,ll per,int pos) {
        if ( len == pos ) {
            sum += per;
            if(sum == val){
                ans++;
            }
            return;
        }
        for (int i = 0;i<3;i++) {
            if ( i == 0 ) {
                if (per>=0) { 
                    dfs(sum,per*10+1LL*(str[pos]-'0'),pos+1);
                } else if (per<0) {
                    dfs(sum,per*10-1LL*(str[pos]-'0'),pos+1);
                }
            } else if (i == 1) {
                dfs(sum+per,1LL*(str[pos]-'0'),pos+1);
            } else if (i == 2) {
                dfs(sum+per,-1LL*(str[pos]-'0'),pos+1);
            }
        }
    }
    
    int main() {
        while(~scanf("%s %lld",str,&val))
        {
            ans = 0;
            len = strlen(str);
            dfs(0,1LL*(str[0]-'0'),1);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    NET在后置代码中输入JS提示语句(背景不会变白)
    陈广老师C#参考视频 方法的参数传递 总结
    preventDefault和stopPropagation两个方法的区别
    zerobased budgeting: 零基预算法
    JS: 关于自执行的匿名函数(整理)
    通过实例理解javascript 的call()与apply()
    setTimeout注意几点
    js constructor
    canphp的数据库操作
    JS事件监听器
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745972.html
Copyright © 2011-2022 走看看