• [华为oj]24点问题


    这里提供自己解二十四点的思路。总的思路就是用类似递归思想:

    (1)在4个数中选两个数进行运算,与另外两个数放在一起,执行步骤(2);

       判断步骤(2)返回结果,为1,返回结果1;为0,继续下一个运算,直至结束,返回结果0;

    (2)在3个数中选两个数进行运算,与另一个数放在一起,执行步骤(3);

        判断步骤(3)返回结果,为1,返回结果1;为0,继续下一个运算,直至结束,返回结果0;

    (3)对两个数进行运算,判断是否为24。是,返回1;否,返回0。

    这个思想就是穷举方法,列出所有可能的情况,有个好处就是不需要考虑括号的情况。代码比较多,但应该比较好理解一点。两个细节:

    1)在两个数进行运算的时候,需要把先后顺序也考虑进去。

    2)当除数为0时,显然不满足情况,因此,返回一个比较大的值。

    具体代码如下:

      1 #include <iostream>
      2 
      3 using namespace std;
      4 
      5 char op[4]={'+','-','*','/'};
      6 
      7 float CalNum(float a,float b,char op)
      8 {
      9     if(b==0)
     10         return 10000;
     11 
     12     float t=0;
     13     switch(op)
     14     {
     15         case '+':
     16             t=a+b;
     17             break;
     18         case '-':
     19             t=a-b;
     20             break;
     21         case '*':
     22             t=a*b;
     23             break;
     24         case '/':
     25             t=a/b;
     26             break;
     27     }
     28     return t;
     29 }
     30 
     31 float Cal2Num(float a[])
     32 {
     33     float result=0;
     34     for(int i=0;i<2;i++)
     35     {
     36         for(int p=0;p<4;p++)
     37         {
     38             result=CalNum(a[i],a[1-i],op[p]);
     39             if(result==24)
     40             {
     41                 return 1;
     42             }
     43         }
     44     }
     45     return 0;
     46 }
     47 
     48 float Cal3Num(float a[])
     49 {
     50     float a2[2];
     51     for(int i3=0;i3<3;i3++)
     52     {
     53         for(int j3=i3+1;j3<3;j3++)
     54         {
     55             for(int k=0;k<3;k++)
     56             {
     57                 if(k!=i3&&k!=j3)
     58                     a2[1]=a[k];
     59             }
     60 
     61             for(int p=0;p<4;p++)
     62             {
     63                 for(int d=0;d<2;d++)
     64                 {
     65                     if(d==0)
     66                         a2[0]=CalNum(a[i3],a[j3],op[p]);
     67                     else
     68                         a2[0]=CalNum(a[j3],a[i3],op[p]);
     69                     if(Cal2Num(a2))
     70                         return 1;
     71                 }
     72             }
     73         }
     74     }
     75     return 0;
     76 }
     77 
     78 float Cal4Num(float a[])
     79 {
     80     float a3[3];
     81     for(int i=0;i<4;i++)
     82     {
     83         for(int j=i+1;j<4;j++)
     84         {
     85             int t=1;
     86             for(int m=0;m<4;m++)
     87             {
     88                 if(m!=i&&m!=j)
     89                     a3[t++]=a[m];
     90             }
     91 
     92             for(int p=0;p<4;p++)
     93             {
     94                 for(int d=0;d<2;d++)
     95                 {
     96                     if(d==0)
     97                         a3[0]=CalNum(a[i],a[j],op[p]);
     98                     else
     99                         a3[0]=CalNum(a[j],a[i],op[p]);
    100                     if(Cal3Num(a3))
    101                         return 1;
    102                 }
    103             }
    104         }
    105     }
    106     return 0;
    107 }
    108 
    109 int main()
    110 {
    111     float a4[4];
    112     cin>>a4[0]>>a4[1]>>a4[2]>>a4[3];
    113     if(Cal4Num(a4))
    114         cout<<"true"<<endl;
    115     else
    116         cout<<"false"<<endl;
    117     return 0;    
    118 }
  • 相关阅读:
    lua类对象
    toLua初始化碰到的问题
    Unity经验之谈-DoTween动画结束匿名委托闭包之坑
    toLua关于委托没有注册的解决方案
    xLua使用require改变路径加载Lua脚本
    unity常用的比较函数
    Shader中颜色混合的算法
    UnityShader中插值平滑曲线
    Shader中的Uniforms(只读标识)
    ShaderLab中Properties语义块支持的属性类型
  • 原文地址:https://www.cnblogs.com/lsr-flying/p/4786365.html
Copyright © 2020-2023  润新知