HDU 2266 How Many Equations Can You Find
来源:互联网 发布:闪电抢购软件怎么样 编辑:程序博客网 时间:2024/06/07 05:02
题目链接:戳这里
题意:给一个字符串,向里面添加“+”或“-”,使之结果等于N。
先自己做了一遍,结果不对,最后发现是我考虑不周全,我的搜索过程,只加上或减去了运算符之后的第一个数字,导致最后结果少了一些(见示例)。正确的方法:搜索到每一位数字时,要加上或减去它之后的所有组合情况。
最初的错误代码:
#include<string>#include<iostream>using namespace std;string str;int len,cnt;long long n;void DFS(int x,int sum){if(x==len){if(sum==n)cnt++;return;}DFS(x+1,sum+(str[x]-'0'));DFS(x+1,sum-(str[x]-'0'));DFS(x+1,sum*10+(str[x]-'0'));}int main(){while(cin>>str>>n){len=str.length();cnt=0;DFS(1,(str[0]-'0'));cout<<cnt<<endl;}return 0;}
AC的代码:
#include<string>#include<iostream>using namespace std;string str;int len,cnt;long long n;void DFS(int x,int sum){if(x==len){if(sum==n)cnt++;return;}long long tmp=0;for(int i=x;i<len;i++){ tmp=tmp*10+(str[i]-'0');DFS(i+1,sum+tmp);if(x)DFS(i+1,sum-tmp);}}int main(){while(cin>>str>>n){len=str.length();cnt=0;DFS(0,0);cout<<cnt<<endl;}return 0;}
0 0
- How Many Equations Can You Find HDU
- hdu 2266 How Many Equations Can You Find
- hdu 2266 How Many Equations Can You Find
- hdu 2266 How Many Equations Can You Find
- HDU 2266 How Many Equations Can You Find 深搜
- 【HDU】2266 - How Many Equations Can You Find(dfs)
- HDU 2266 How Many Equations Can You Find (DFS)
- HDU-2266-How Many Equations Can You Find【交叉DFS】
- HDOJ(HDU).2266 How Many Equations Can You Find (DFS)
- hdu 2266 How Many Equations Can You Find
- HDU 2266 How Many Equations Can You Find
- hdu-2256 How Many Equations Can You Find
- How Many Equations Can You Find
- How Many Equations Can You Find
- hdu2266 How Many Equations Can You Find
- How Many Equations Can You Find(DFS)
- How Many Equations Can You Find
- How Many Equations Can You Find
- freemarker与springMvc整合的坑
- 初学者使用hibernate连接MySQL出现unable to create connection check your url问题
- 两种排序方法:冒泡排序和插入排序
- Linux查看文件详细信息指令stat
- 五、JVM调优指令
- HDU 2266 How Many Equations Can You Find
- 解决service 'visualSVN Server' failed to start.please check visualSVN server log
- Modbus转profinet网关在天津燃油公司的应用案例
- python从Mjpg-streamer中取出一张jpg照片
- Java面试-进阶(一)
- 安装QWT
- 判断子网掩码是否错误的小程序
- L1-009. N个数求和
- 常用数据库 JDBC URL 格式