现在的位置: 首页 > 综合 > 正文

hdu(2266)深搜

2013年08月06日 ⁄ 综合 ⁄ 共 429字 ⁄ 字号 评论关闭
本题要求在原字符串中添加一些+、-号,使所得的值所给定值相同。
这提示到很明显的搜索题。注意搜索的方法。
 
 
 
#include"stdio.h"
#include"string.h"
__int64 count,m;
char a[100];
int b[100],k;
void dfs(int x,__int64 y)
{
 int i;
 if(x==k&&y==m)
 {
  count++;
  return ;
 }
 __int64 h=0;//h定义不能放在题头,不然会输出错误的结果。。
 for(i=x;i<k;i++)
 {
  h=h*10+a[i]-'0';
  dfs(i+1,y+h);
  if(x!=0)
  dfs(i+1,y-h);
 }
}
int main()
{
 while(scanf("%s%I64d",a,&m)!=EOF)
 {
  k=strlen(a);
  count=0;
  dfs(0,0);
  printf("%I64d\n",count);
 }
 return 0;
}

抱歉!评论已关闭.