Use the BFS to solve the equation problems(Case : hdu 4403 )
A very hard Aoshu problem
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
2012 ACM/ICPC Asia Regional Jinhua Online
Problem analysis:
Give you a a digit serial,and you task is add one or more "+"and only one "=" to the digit serial to make up a equation.
For this problem,we can use the BFS to solve it.But the key problem is how to handle it.
Here,provide a solution to the problem.
First,we consider all kinds of conditions before "=",and then after "=";if the two parts values are the same,add 1 to the calculation resultss.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include<iostream> #include<string> #include<algorithm> #include<queue> using namespace std; char str[30]; //读入 int num[30]; //转换为数字 int ans; //统计结果 int len; //计算读入字符数组的长度 void BFS_count( int j, int sum, int sum2, int p) //sum代表“=”左边的值 sum2代表右边的值 { if (j==len) //如果遍历等号右边所有的数 可以开始判断是否相等 { if (sum==sum2) ans++; return ; } p=p*10+num[j]; BFS_count(j+1,sum,sum2+p,0); //遍历“=”号右边的数插入等号的情况 if (j+1!=len) //如果没有遍历到最后一个数继续遍历 BFS_count(j+1,sum,sum2,p); //遍历“=”号右边的数不插入等号的情况 } void BFS( int j, int sum, int p) //j代表插入符号的位置 { if (j==len-1) return ; p=p*10+num[j]; BFS(j+1,sum+p,0); //搜索等号左边插入加号的情况 BFS(j+1,sum,p); //搜索等号左边不插入加号的情况 BFS_count(j+1,sum+p,0,0); //遍历以上所有情况 } int main() { while ( gets (str)) { if ( strcmp (str, "END" )==0) break ; len= strlen (str); int i; for (i=0;str[i]!= '\0' ;i++) num[i]=str[i]- '0' ; ans=0; BFS(0,0,0); cout<<ans<<endl; } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?