HDU 4403 A very hard Aoshu 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

 

数据小,直接枚举全部情况即可

#include<iostream>
#include<cstdlib>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
char s[maxn];
int n,ans,vis[maxn];

int main()
{
	int i,j,k;
    while (~scanf("%s",s),strcmp(s,"END"))
    {
        n=strlen(s);    ans=0;
		for ( j=0;j<n-1;j++)
        {
			for ( i=0;i<(1<<n);i++) vis[i]=0;
			for ( i=0;i<(1<<n);i++)
			if(!vis[i|(1<<j)])
			{    
				vis[i|(1<<j)]=1;
                __int64 q=0,h=0,bef=0;
                for ( k=0;k<=j;k++)
                {
                    bef=bef*10+s[k]-'0';
                    if ((i&(1<<k))||k==j) q+=bef,bef=0;
                }
                bef=0;
                for ( k=j+1;s[k];k++)
                {
                    bef=bef*10+s[k]-'0';
                    if ((i&(1<<k))||!s[k+1]) h+=bef,bef=0;
                }
                if (q==h) 
				{
					ans++;
					//printf("%d %d %d\n",q,i,j);
				}
            }
        }
        printf("%d\n",ans/2);
    }
    return 0;
}


你可能感兴趣的:(HDU 4403 A very hard Aoshu problem)