zoukankan      html  css  js  c++  java
  • A very hard Aoshu problem(dfs或者数位)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403

    A very hard Aoshu problem

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


    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
    题解:考虑枚举等号的位置,每次dfs等号左侧的值,对应找等号右边是否有对应的相同值,这里一定要注意调用的时候的细节,就是考虑每个位置的标号
    也可以将每个数字之间的位置用二进制表示,枚举完等号后,其他的位置0表示不放+号,1表示放
    dfs代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 using namespace std;
     5 char str[17];
     6 int val[17][17];
     7 int len;
     8 int ans;
     9 
    10 void cal()
    11 {
    12     memset(val,0,sizeof(val));
    13     for(int i = 0 ; i < len ; i++)
    14         for(int j = i ; j < len ; j++)
    15             for(int k = i ; k <= j ; k++)
    16                 val[i][j] = val[i][j]*10+(str[k]-'0');
    17 }
    18 void dfsr(int lsum,int pos, int rsum)
    19 {
    20     if(pos>=len)
    21     {
    22         if(rsum==lsum)
    23             ans++;
    24             return ;
    25     }
    26     for(int k = pos+1 ; k <= len ; k++)
    27         dfsr(lsum,k,rsum+val[pos][k-1]);
    28 }
    29 void dfsl(int equ , int pos , int lsum)
    30 {
    31     if(pos>=equ)
    32         dfsr(lsum,equ,0);
    33         for(int k = pos+1 ; k <= equ ; k++)
    34             dfsl(equ,k,lsum+val[pos][k-1]);
    35 }
    36 
    37 int main()
    38 {
    39     while(~scanf("%s",str)&&str[0]!='E')
    40     {
    41         ans = 0 ;
    42         len = strlen(str);
    43         cal();
    44         int equ;
    45         for(equ = 1 ; equ < len ; equ++)
    46             dfsl(equ,0,0);
    47         printf("%d
    ",ans);
    48     }
    49     return 0;
    50 }

     数位:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 using namespace std;
     5 #define N 17
     6 char a[N];
     7 int equ;
     8 int len;
     9 bool ck(int sum)
    10 {
    11     int l= 0 , r=0;
    12     int cur = 0;
    13     for(int i = 0  ; i <= equ ;i++)
    14     {
    15         if(sum&(1<<i)) {
    16             cur = cur * 10 + a[i]-'0';
    17             l+=cur;
    18             cur = 0;
    19         }
    20         else cur = cur*10+a[i]-'0';
    21     }
    22     if(cur != 0) l+=cur; 
    23     cur = 0 ;
    24     for(int i = equ+1 ; i< len ; i++)
    25     {
    26         if(sum&(1<<i)){
    27             cur = cur * 10 + a[i]-'0';
    28             r+=cur;
    29             cur = 0;
    30         }
    31         else cur = cur*10+a[i]-'0';
    32     }
    33     if(cur!=0) r += cur;
    34     if(l==r) return true;
    35     else return false;
    36 }
    37 int main()
    38 {
    39     while(~scanf("%s",a)&&a[0]!='E')
    40     {
    41         int ans = 0 ;
    42         len = strlen(a);
    43         for( equ = 0 ; equ < len-1 ; equ++)
    44         {
    45             int tm = 1<<(len-1);
    46             for(int j = 0 ; j < tm ;j++)
    47             {
    48                 if(ck(j)){
    49                     ans++;
    50             //        printf("%d %d
    ", equ, j);
    51                 }
    52             }
    53         }
    54         printf("%d
    ",ans>>1);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Nginx的configure各项中文说明
    nginx+keepalived高可用及双主模式
    nginx实现请求的负载均衡 + keepalived实现nginx的高可用
    Keepalived详细介绍简介
    keepalived vip漂移基本原理及选举算法
    keepalived脑裂问题查找
    计算机网络的三种通讯模式(单播,广播,组播)
    二层交换机、三层交换机、路由器
    Linux /var/log下的各种日志文件详解
    报文、帧、数据包等的区别
  • 原文地址:https://www.cnblogs.com/shanyr/p/4750337.html
Copyright © 2011-2022 走看看