zoukankan      html  css  js  c++  java
  • HDU 2266 How Many Equations Can You Find(DFS)

    How Many Equations Can You Find

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    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

    简单翻译:

    给你两个数n,m.你需要在n的中间填上加号或减号,或者不填。让n这个部分的值等于m。问有多少种方案。

    举个例子:n=12345.

    你可以操作它,使它变成这样 123+4-5=122。

    解题思路:

    dfs,考虑已经处理的数字的个数和当前这部分的值,处理到终点时,如果等于m,结果+1.

    代码:

    #include<cstdio>
    #include<cstring>
    #define LL long long
    using namespace std;
    char String[120];
    LL Equation;
    int Length,Answer;
    void Search_For_Answer(int Now_Position,LL Now_Value)
    {
        if(Now_Position>=Length)
        {
            if(Now_Value==Equation) Answer++;
            return;
        }
        LL Temp_Value=0;
        for(int i=1;i<=Length-Now_Position;i++)
        {
            Temp_Value=Temp_Value*10+String[i-1+Now_Position]-'0';
            Search_For_Answer(Now_Position+i,Now_Value+Temp_Value);
            if(Now_Position)
                Search_For_Answer(Now_Position+i,Now_Value-Temp_Value);
        }
    }
    int main()
    {
        while(scanf("%s%lld",String,&Equation)!=EOF)
        {
            Answer=0;
            Length=strlen(String);
            Search_For_Answer(0,0);
            printf("%d
    ",Answer);
        }
        return 0;
    }
  • 相关阅读:
    IDirect3DDevice9::Clear
    Width vs Pitch
    5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
    3- vue django restful framework 打造生鲜超市
    2- vue django restful framework 打造生鲜超市 -环境搭建
    1- vue django restful framework 打造生鲜超市
    Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章
    windows10上安装mysql
    博客开通第一天
  • 原文地址:https://www.cnblogs.com/I-love-HLD/p/4624846.html
Copyright © 2011-2022 走看看