博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4403:A very hard Aoshu problem(DFS暴搜)
阅读量:6239 次
发布时间:2019-06-22

本文共 2367 字,大约阅读时间需要 7 分钟。

detail:http://blog.csdn.net/sr_19930829/article/details/39206145?utm_source=tuicool&utm_medium=referral

A very hard Aoshu problem

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


Problem Description
Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:
Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.
 

Input
There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".
 

Output
For each test case , output a integer in a line, indicating the number of equations you can get.
 

Sample Input
 
1212 12345666 1235 END
 

Sample Output
 
2 2 0
 

Source
题意:给定一个字符串,要求插入一个=号和若干个+号使式子成立。

思路:枚举等号的位置,依次将等号左边的所有情况计算出来同时计算等号右边的看是否匹配。

# include 
# include
char s[20];int num[20][20], ans, len;void init(){ for(int i=1; i<=len; ++i) { num[i][i] = s[i]-'0'; for(int j=i+1; j<=len; ++j) num[i][j] = num[i][j-1]*10 + s[j] - '0'; }}void dfsr(int pos, int leftsum, int sum){ if(pos > len) { if(sum == leftsum) ++ans; return; } for(int i=pos; i<=len; ++i) dfsr(i+1, leftsum, sum+num[pos][i]);}void dfsl(int equ, int pos, int sum){ if(pos > equ) dfsr(equ+1, sum, 0); for(int i=pos; i<=equ; ++i) dfsl(equ, i+1, sum+num[pos][i]);}int main(){ while(~scanf("%s",s+1)) { if(s[1] == 'E') break; len = strlen(s+1); memset(num, 0, sizeof(num)); ans = 0; init(); for(int i=1; i<=len; ++i) dfsl(i, 1, 0); printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6730061.html

你可能感兴趣的文章
编译安装部署最新C++开发环境
查看>>
DNS服务
查看>>
ActiveMQ(21):Consumer高级特性之管理持久化订阅
查看>>
ActiveMQ(15):Message Dispatch的消息游标与异步发送
查看>>
Kubernetes+Etcd-v1.5.2 分布式集群部署
查看>>
表连接
查看>>
Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法...
查看>>
我的友情链接
查看>>
“对人不对事”和“对事不对人”
查看>>
CSS实现pre标签中内容换行方法
查看>>
领导牛B轰轰的 https 介绍应用
查看>>
Centos7下的Openssl和CA
查看>>
windows 2003防止网卡被禁用的设置
查看>>
lvs健康检测脚本
查看>>
马哥运维架构 第五周作业
查看>>
如何使用netstat命令验证DDOS***
查看>>
【zabbix学习笔记之三】部署zabbix-agent端
查看>>
找到数组中第i大、第i小的值
查看>>
我的友情链接
查看>>
ceph源码片段——c++引用
查看>>