首页 > 代码库 > HDU 4403 无脑暴搜

HDU 4403 无脑暴搜

A very hard Aoshu problem

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


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
 
 
 
题目意思:
给一串数字,在数字钟插入‘+‘或者‘=‘,若等号两边相等,那么ans++,输出ans。
 
思路:
数字串最多为15个,那么在每个位置处什么都不插,插入‘+‘,插入‘=‘,dfs一下就行了。
 
代码太丑:
 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8  9 char s[40], a[40];10 int ans;11 12 void dfs(int l,int r,int id,int f){13     int i, j, k;14     if(l>r){15     //    a[id]=s[strlen(s)-1];16         int ff=0, num1, num2, num3, num4;17         num1=num2=num3=num4=0;18         19         for(i=0;i<id;i++){20             if(a[i]>=0&&a[i]<=9){21                 if(!ff)22                 num2=num2*10+a[i]-0;23                 else num3=num3*10+a[i]-0;24             }25             else if(a[i]==+){26                 if(!ff){27                     num1+=num2;28                 num2=0;29                 }30                 else {31                     num4+=num3;num3=0;32                 }33             }34             else if(a[i]===){35                 ff=1;36             }37         }38         if(num2!=0) num1+=num2;39         if(num3!=0) num4+=num3;40         if(num1==num4&&num1!=0){41             ans++;//for(i=0;i<id;i++) printf("%c",a[i]);42         //cout<<endl<<endl;43         } 44         return;45     }46     a[id++]=s[l];47         dfs(l+1,r,id,f);48         id--;49         if(a[id-1]!=+&&id!=0&&a[id-1]!==&&l<=r){50             a[id++]=+;51         dfs(l,r,id,f);52         id--;53         }54         55         if(!f&&id!=0&&a[id-1]!=+&&l<=r){56             a[id++]==;f=1;57             dfs(l,r,id,f);58             f=0;id--;59         }60 }61 main()62 {63     int i, j, k;64     while(scanf("%s",&s)!=EOF&&strcmp(s,"END")){65         j=strlen(s);66         ans=0;67         dfs(0,j-1,0,0);68         printf("%d\n",ans);69     }70 }

 

HDU 4403 无脑暴搜