美文网首页
关于24点游戏的一个简单算法-iOS

关于24点游戏的一个简单算法-iOS

作者: 生死有命斧王 | 来源:发表于2019-05-07 13:42 被阅读0次

    先利用穷举法,例出所有计算公式。

    -(NSString *)calculate:(CGFloat)x y:(CGFloat)y z:(CGFloat)z w:(CGFloat)w{
        NSString *str = @"";
        if (x+y+z+w==24)            str = [NSString stringWithFormat:@"%.0f+%.0f+%.0f+%.0f",x,y,z,w];
        
        else if (x+y+z-w==24)       str = [NSString stringWithFormat:@"%.0f+%.0f+%.0f-%.0f",x,y,z,w];
        
        else if (x+y-z-w==24)       str = [NSString stringWithFormat:@"%.0f+%.0f-%.0f-%.0f",x,y,z,w];
        
        else if ((x+y)*(z+w)==24)   str = [NSString stringWithFormat:@"(%.0f+%.0f)×(%.0f+%.0f)",x,y,z,w];
        
        else if ((x-y)*(z+w)==24)   str = [NSString stringWithFormat:@"(%.0f-%.0f)×(%.0f+%.0f)",x,y,z,w];
        
        else if ((x-y)*(z-w)==24)   str = [NSString stringWithFormat:@"(%.0f-%.0f)×(%.0f-%.0f)",x,y,z,w];
        
        else if ((x+y+z)*w==24)     str = [NSString stringWithFormat:@"(%.0f+%.0f+%.0f)×%.0f",x,y,z,w];
        
        else if ((x-y-z)*w==24)     str = [NSString stringWithFormat:@"(%.0f-%.0f-%.0f)×%.0f",x,y,z,w];
        
        else if ((x+y-z)*w==24)     str = [NSString stringWithFormat:@"(%.0f+%.0f-%.0f)×%.0f",x,y,z,w];
        
        else if ((x*y*z)/w==24)     str = [NSString stringWithFormat:@"(%.0f×%.0f×%.0f)÷%.0f",x,y,z,w];
        
        else if ((x*y)*(z+w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)×(%.0f+%.0f)",x,y,z,w];
        
        else if ((x*y)*(z-w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)×(%.0f-%.0f)",x,y,z,w];
        
        else if ((x*y)*z-w==24)     str = [NSString stringWithFormat:@"%.0f×%.0f×%.0f-%.0f",x,y,z,w];
        
        else if ((x*y)*z+w==24)     str = [NSString stringWithFormat:@"%.0f×%.0f×%.0f+%.0f",x,y,z,w];
        
        else if (x*y*z*w==24)       str = [NSString stringWithFormat:@"%.0f×%.0f×%.0f×%.0f",x,y,z,w];
        
        else if ((x+y)+(z/w)==24)   str = [NSString stringWithFormat:@"(%.0f+%.0f)+(%.0f÷%.0f)",x,y,z,w];
        
        else if ((x+y)*(z/w)==24)   str = [NSString stringWithFormat:@"(%.0f+%.0f)×(%.0f÷%.0f)",x,y,z,w];
        
        else if ((x*y)+z+w==24)     str = [NSString stringWithFormat:@"%.0f×%.0f+%.0f+%.0f",x,y,z,w];
        
        else if ((x*y)+z-w==24)     str = [NSString stringWithFormat:@"%.0f×%.0f+%.0f-%.0f",x,y,z,w];
        
        else if ((x*y)-(z/w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)-(%.0f÷%.0f)",x,y,z,w];
        
        else if ((x*y)+(z/w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)+(%.0f÷%.0f)",x,y,z,w];
        
        else if ((x*y)-z-w==24)     str = [NSString stringWithFormat:@"(%.0f×%.0f)-%.0f-%.0f",x,y,z,w];
        
        else if ((x*y)+(z*w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)+(%.0f×%.0f)",x,y,z,w];
        
        else if ((x*y)-(z*w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)-(%.0f×%.0f)",x,y,z,w];
        
        else if ((x*y)/(z*w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)÷(%.0f×%.0f)",x,y,z,w];
        
        else if ((x*y)/(z-w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)÷(%.0f-%.0f)",x,y,z,w];
        
        else if ((x*y)/(z+w)==24)   str = [NSString stringWithFormat:@"(%.0f×%.0f)÷(%.0f×%.0f)",x,y,z,w];
        
        return str;
    }
    

    再利用循环例举出4个数字的所有排列组合,调用calculate方法就能得到24点的解法了。

    CGFloat a = 1,b = 2,c = 3,d = 4;
        NSArray *arr = @[[NSNumber numberWithFloat:a],[NSNumber numberWithFloat:b],[NSNumber numberWithFloat:c],[NSNumber numberWithFloat:d]];
        //共四个循环
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(j==i)//除去j==i的情况,下同
                    continue;
                for(int k=0;k<4;k++){
                    if(k==j||k==i)
                        continue;
                    for(int h=0;h<4;h++){
                        if(h==k||h==j||h==i)
                            continue;
                        NSString *str = [self calculate:[((NSNumber *)arr[i]) floatValue] y:[((NSNumber *)arr[j]) floatValue] z:[((NSNumber *)arr[k]) floatValue] w:[((NSNumber *)arr[h]) floatValue]];
                        if(str.length>0){
                            text.text = str;
                            return;
                        }
                    }
                }
            }
        }
    
    算法参考链接:https://blog.csdn.net/wangqiulin123456/article/details/8145545
    关于字符串转计算公式:JHStringFormula

    相关文章

      网友评论

          本文标题:关于24点游戏的一个简单算法-iOS

          本文链接:https://www.haomeiwen.com/subject/sieeoqtx.html