smalltian
没有不努力的大神,只有不努力的菜鸟

 

                                   A very hard Aoshu problem

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

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
 
网上看了某位大神的代码,挺犀利的。按照他的思路又重新自己写了一遍,他是把等号左右两边加号所有可能存在的位置用二进制来表示,用来处理。
 1 #include<iostream>
 2 using namespace std;
 3 int num[20];
 4 int len_str;
 5 int sum;
 6 bool solve(int plus_front,int plus_behind,int equal)
 7 {
 8     int i,j;
 9     int add_front=0,add_behind=0;
10     int temp;
11     temp=num[1];
12     for(i=1;i<equal;i++)             //求出等号前面的数的和
13     {
14         int examine = 1<<(i-1);              
15         if(examine & plus_front)
16         {
17             add_front+=temp;
18             temp=num[i+1];
19         }
20         else
21         {
22             temp*=10;
23             temp+=num[i+1];
24         }
25     }
26     add_front+=temp;
27     temp=num[i+1]; 
28     for(i=equal+1;i<len_str;i++)         //求出等号右边的数的和
29     {
30         int examine = 1<<(i-equal-1);
31         if( examine & plus_behind)
32         {
33             add_behind+=temp;
34             temp=num[i+1];
35         }
36         else
37         {
38             temp*=10;
39             temp+=num[i+1];
40         }
41     }
42     add_behind+=temp;
43     if(add_behind==add_front)
44         return true;
45     else
46         return false;
47 }
48 int main()
49 {
50     freopen("in.txt","r",stdin);
51     char str[20];
52     while(cin>>str)
53     {
54         if(strcmp(str,"END")==0)
55             break;
56         memset(num,0,sizeof(num));
57         sum=0;
58         len_str=strlen(str);
59         int i,j;
60         for(i=0,j=1;i<len_str;i++)
61             num[j++]=str[i]-'0';
62         for(i=1;i<len_str;i++)                      //遍历等号可能所在的位置
63         {
64             int plus_front_all  = 1<<(i-1);         //在等号左边,加号所有可能的位置
65             int plus_behind_all = 1<<(len_str-i-1); //在等号右边,加号所有可能的位置
66             for(j=0;j<plus_front_all;j++)
67                 for(int k= 0;k<plus_behind_all;k++)
68                     if(solve(j,k,i))
69                         sum++;
70         }
71         cout<<sum<<endl;
72     }
73     return 0;
74 }

 

 
posted on 2012-11-23 20:26  smalltian  阅读(190)  评论(0)    收藏  举报