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): 537    Accepted Submission(s): 368


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
 
题意:
输入一个数字 把数字化成 + 和=组成的式子  比如 1212 可以化成 1+2=1+2   12=12      注意12=21和 21=12是不一样的
思路:
DFS暴力出所有数字组合   然后分成2部分 看能有多少个组合即可
 
#include<stdio.h>
#include<string.h>
int cnt,flag[22],num[22],len;
char s[22];///flag[i]=1表示把i之前包括i的数字合成一个数字分开 分出一个数字来
void DFS(int id)
{
    int i,j,k;
    if(id==len-1)
    {
        int c=0,mid=0;
         for(i=0;i<len;i++)
         {
             if(flag[i]==1)
             {
                   mid=mid*10+s[i]-'0';
                   num[c++]=mid;
                   mid=0;
             }
             else
             {
                 mid=mid*10+s[i]-'0';
             }
         }
         if(mid!=0)  num[c++]=mid;
         int left=0,right=0;
         for(k=0;k<c;k++)
         {
            // printf("num[%d]=%d \n",k,num[k]);
             left=right=0;
             for(i=0;i<=k;i++) left+=num[i];
             for(i=k+1;i<c;i++) right+=num[i];
             if(left==right) cnt++;
         }
         return;

    }
    flag[id]=1;
    DFS(id+1);
    flag[id]=0;
    DFS(id+1);
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        if(strcmp(s,"END")==0) break;
        len=strlen(s);
        memset(flag,0,sizeof(flag));
        cnt=0;
        DFS(0);
        printf("%d\n",cnt);
    }
    return 0;
}

由于数据不大
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值