hdu—4403

A very hard Aoshu problem

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


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   |   We have carefully selected several similar problems for you:   6010  6009  6008  6007  6006 

【分析】
乍一看以为是数位dp...不过其实没那么复杂,因为每个字符串长度最大只有15,而且只有一个=,所以直接暴搜就可以,这里有个小优化,预处理一下[i,j]这个数字是多少,然后把整个字符串分成两段,暴搜左边的能出现的数字在右边能不能出现就可以了。
【代码】
#include <cstdio>  
#include <cstring>  
char s[100];  
int len,ans;  
int f[100][100];  
void findd(int deep,int sum,int summ)  
{  
    if(deep==len)  
    {  
        if(sum==summ) ans++;  
        return;  
    }  
    for(int i=deep;i<len;i++)  
        findd(i+1,sum+f[deep][i],summ);  
}  
void find(int deep,int end,int sum)  
{  
    if(deep==end) findd(end,0,sum);  
    for(int i=deep;i<end;i++)  
        find(i+1,end,sum+f[deep][i]); 
}  
int main()  
{  
/*	FILE *fp1, *fp2;
	fp1 = fopen("sample.in","r");
	fp2 = fopen("sample.out","w");
 */
 	while(~fscanf(fp1,"%s",s))  
    {  
        if(s[0]=='E') break;  
        memset(f,0,sizeof(f));  
        len=strlen(s);  
        ans=0;  
        for(int i=0;i<len;i++)  
            for(int j=i;j<len;j++)    
                for(int k=i;k<=j;k++)  
                    f[i][j]=f[i][j]*10+s[k]-48;  
        for(int i=1;i<len;i++)  
            find(0,i,0);  
        fprintf(fp2,"%d\n",ans);  
    }  
/*  fclose(fp1);
 	fclose(fp2);
*/    return 0;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值