码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2266 dfs

时间:2017-03-09 00:05:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:cee   添加   ring   make   can   man   mission   lin   val   

How Many Equations Can You Find

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985    Accepted Submission(s): 659


Problem 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
 大意:
给出一个字符串,往任意二个字符间添加‘+’‘-‘,使得其值等于给定的aim,求方案个数,字符串前后均无符号;
自己第一次使用dfs保存一个加工过的字符串,再用一个pd函数判定此字符串的和;
其实可以直接暴力dfs水果,更省时:                      //ps:55555555~~~感觉自己好傻

#include<bits/stdc++.h>
using namespace std;
char num[15],b[105];
int aim,ans,n;
void dfs(int cur,int sum)
{
if(cur==n){
if(sum==aim) ans++;
return;
}
int t=0;
for(int i=cur;i<n;i++){
t=t*10+num[i]-‘0‘;                         //其实就是暴力枚举出所有的+-情况
dfs(i+1,sum+t);
if(cur>0) dfs(i+1,sum-t);
}
}


int main()
{
while(cin>>num>>aim/*scanf("%s%d",num,&aim)!=EOF*/){
n=strlen(num);
ans=0;
dfs(0,0);
printf("%d\n",ans);
}
return 0;
}

例如 1234
首先

hdu 2266 dfs

标签:cee   添加   ring   make   can   man   mission   lin   val   

原文地址:http://www.cnblogs.com/zzqc/p/6523527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!