HDU 2266 How Many Equations Can You Find(模拟,深搜)

题目

 

这是传说中的深搜吗。。。。不确定,,,,貌似更加像是模拟,,,,

 

//我要做深搜题目拉
//实际上还是模拟

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int ans;
char a[20];
__int64 n;
int fuhao[20];
int len;
__int64 cal()
{
    int fh=0;
    __int64 num=0,tmp=0;
    tmp=a[0]-'0';
    for(int i=1;i<len;i++){
        if(fuhao[i-1]==1){
            if(fh==1)num+=tmp;
            else if(fh==2)num-=tmp;
            else if(fh==0)num=tmp;
            tmp=0;
            fh=1;
        }
        else if(fuhao[i-1]==2){
            if(fh==1)num+=tmp;
            else if(fh==2)num-=tmp;
            else if(fh==0)num=tmp;
            tmp=0;
            fh=2;
        }
        tmp=tmp*10+a[i]-'0';
    }
    if(fh==1)num+=tmp;
    else if(fh==2)num-=tmp;
    else num=tmp;
    return num;
}
void dfs(int st)
{
    if(st==len-1){//符号比数少一位
        if(cal()==n)ans++;
        return;
    }
    fuhao[st]=0;
    dfs(st+1);
    fuhao[st]=1;
    dfs(st+1);
    fuhao[st]=2;
    dfs(st+1);
}

int main()
{
    while(scanf("%s%I64d",a,&n)!=EOF)
    {
        memset(fuhao,0,sizeof(fuhao));
        len=strlen(a);
        ans=0;
        dfs(0);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2014-09-19 22:28  laiba2004  Views(125)  Comments(0Edit  收藏  举报