当前位置:编程学习 > C/C++ >>

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
2012 ACM/ICPC Asia Regional Jinhua Online
 


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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,