24点算法

24点解法之一:
通过穷举:
TWTequalNum 是一个宏 24
-(NSString *)calculate:(int)x y:(int)y z:(int)z w:(int)w{
NSString *str = @"";
if (x+y+z+w== TWTequalNum)
{
str = [NSString stringWithFormat:@"%d+%d+%d+%d",x,y,z,w];
[self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
[self.txtarr addObject:str ];
}

if(x+y+z-w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d+%d+%d-%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if(x+y-z-w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d+%d-%d-%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x+y)*(z+w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d+%d)×(%d+%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x-y)*(z+w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d-%d)×(%d+%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x-y)*(z-w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d-%d)×(%d-%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x+y+z)*w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d+%d+%d)×%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x-y-z)*w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d-%d-%d)×%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x+y-z)*w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d+%d-%d)×%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y*z)/w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d×%d)÷%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)*(z+w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)×(%d+%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)*(z-w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)×(%d-%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)*z-w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d×%d×%d-%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)*z+w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d×%d×%d+%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if(x*y*z*w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d×%d×%d×%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x+y)+(z/w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d+%d)+(%d÷%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x+y)*(z/w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d+%d)×(%d÷%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)+z+w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d×%d+%d+%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)+z-w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"%d×%d+%d-%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)-(z/w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)-(%d÷%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)+(z/w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)+(%d÷%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)-z-w==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)-%d-%d",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)+(z*w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)+(%d×%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)-(z*w)==  TWTequalNum)
    
{
    str = [NSString stringWithFormat:@"(%d×%d)-(%d×%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)/(z*w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)÷(%d×%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)/(z-w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)÷(%d-%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

if((x*y)/(z+w)==  TWTequalNum)
{
    str = [NSString stringWithFormat:@"(%d×%d)÷(%d×%d)",x,y,z,w];
    [self.txtarrNum addObject:[NSString stringWithFormat:@"%d,%d,%d,%d",x,y,z,w]];
    [self.txtarr addObject:str ];
}

return str;

}

你可能感兴趣的:(iOS)