HDU 4403 A very hard Aoshu problem

A very hard Aoshu problem

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


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
 
 
   
Recommend
zhoujiaqi2010
水过,暴力枚举


#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> using namespace std; char s[22]; int t[22]; int n; int ans; void dfs(int id) { if(id==n-1) { int l=0,r=0,tp=0; bool flag=0; for(int i=0;i<n;i++) { tp=tp*10+s[i]-'0'; if(t[i]==1) { if(!flag) l+=tp; else r+=tp; tp=0; } else if(t[i]==2) { flag=1; l+=tp; tp=0; } else if(t[i]==4) r+=tp; } if(l==r) ans++; return ; } if(t[id]==2) id++; if(id==n-1) { int l=0,r=0,tp=0; bool flag=0; for(int i=0;i<n;i++) { tp=tp*10+s[i]-'0'; if(t[i]==1) { if(!flag) l+=tp; else r+=tp; tp=0; } else if(t[i]==2) { flag=1; l+=tp; tp=0; } else r+=tp; } if(l==r) ans++; return ; } dfs(id+1); t[id]=1; dfs(id+1); t[id]=0; } int main() { while(scanf("%s",s)) { if(strcmp(s,"END")==0) break; n=strlen(s); ans=0; for(int i=0;i<n-1;i++) { memset(t,0,sizeof(t)); t[n-1]=4; t[i]=2; dfs(0); } printf("%d\n",ans); } return 0; }

 

你可能感兴趣的:(HDU)