• A very hard Aoshu problem


    A very hard Aoshu proble

    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
                                                                              
     
    由于n小于=15,故暴力枚举即可。又由于只有数字1~9组成,无需考虑几个相邻的0的情况。很简单的一道水题,我们用二进制来枚举。水过。
     
     
     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 using namespace std;
     8 char str[30];
     9 int get(int i,int j,int x,int cnt)
    10 {
    11     int ret,a,b,c;
    12     int s=i;
    13     ret=a=0;
    14     for(a=0;a<cnt;a++)
    15     {
    16         if((1<<a)&x)
    17         {
    18             c=0;
    19             for(b=s;b<=i+a;b++)
    20             {
    21                 c=c*10+str[b]-'0';
    22             }
    23             s=i+a+1;
    24             ret+=c;
    25             
    26         }
    27     }
    28     c=0;
    29     for(b=s;b<=j;b++)
    30         c=c*10+str[b]-'0';
    31     ret+=c;
    32     return ret;
    33 }
    34 int main()
    35 {
    36     int i,j,k,ans,a,b,c,len;
    37     while(scanf("%s",str)&&str[0]!='E')
    38     {
    39         len=strlen(str);
    40         ans=0;
    41         for(i=1;i<len;i++)
    42         {
    43             for(j=0;j<(1<<(i-1));j++)
    44             {
    45 
    46                 for(k=0;k<(1<<(len-i-1));k++)
    47                 {
    48                     a=get(0,i-1,j,i-1);
    49                     b=get(i,len-1,k,len-i-1);
    50                     if(a==b)
    51                     {
    52                         ++ans;
    53                     }
    54                 }
    55 
    56             }
    57         }
    58         cout<<ans<<endl;
    59     }
    60     return 0;
    61 }
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    可惜老板不看程序员写的书
    《让僵冷的翅膀飞起来》系列之四——Visitor模式之可行与不可爱
    《让僵冷的翅膀飞起来》系列之二——从实例谈Adapter模式
    策略模式的应用实践
    《让僵冷的翅膀飞起来》系列之三——从Adapter模式到Decorator模式
    用Design+Blend轻松制作环形文字
    silverlight3的"伪"3D续图片横向轮换
    silverlight.net官方网站图片切换源码
    css2.1中的属性选择器(css高手请绕道)
    多线程中的ManualResetEvent
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4115686.html
Copyright © 2020-2023  润新知