程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4403 A very hard Aoshu problem 輸入數字化成等式形式 好題

hdu 4403 A very hard Aoshu problem 輸入數字化成等式形式 好題

編輯:C++入門知識

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved