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

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

-(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)