#pragma mark - 组合计算公式
- (NSMutableArray *)zuHeSuanFa:(NSMutableArray *)array chooseCount:(int)m
{
int n = [array count];
if (m > n)
{
return nil;
}
NSMutableArray *allChooseArray = [[NSMutableArray alloc] init];
NSMutableArray *retArray = [array copy];
// (1,1,1,0,0)
for(int i = 0;i < n;i++)
{
if (i < m)
{
[array replaceObjectAtIndex:i withObject:@"1"];
}
else
{
[array replaceObjectAtIndex:i withObject:@"0"];
}
}
NSMutableArray *firstArray = [[NSMutableArray alloc] init];
for(int i = 0; i<n; i++)
{
if ([[array objectAtIndex:i] intValue] == 1)
{
// [firstArray addObject:[NSString stringWithFormat:@"%d",i+1]];
[firstArray addObject:[retArray objectAtIndex:i]];
}
}
[allChooseArray addObject:firstArray];
// [firstArray release];
int count = 0;
for(int i = 0; i < n-1; i++)
{
if ([[array objectAtIndex:i] intValue] == 1 && [[array objectAtIndex:(i + 1)] intValue] == 0)
{
[array replaceObjectAtIndex:i withObject:@"0"];
[array replaceObjectAtIndex:(i + 1) withObject:@"1"];
// i = 2, (1,1,0,1,0)
for (int k = 0; k < i; k++)
{
if ([[array objectAtIndex:k] intValue] == 1)
{
count ++;
}
}
if (count > 0)
{
for (int k = 0; k < i; k++)
{
if (k < count)
{
// k = 1, (1,1,0,1,0)
[array replaceObjectAtIndex:k withObject:@"1"];
}
else
{
[array replaceObjectAtIndex:k withObject:@"0"];
}
}
}
NSMutableArray *middleArray = [[NSMutableArray alloc] init];
for (int k = 0; k < n; k++)
{
if ([[array objectAtIndex:k] intValue] == 1)
{
// [middleArray addObject:[NSString stringWithFormat:@"%d",k + 1]];
[middleArray addObject:[retArray objectAtIndex:k]];
}
}
[allChooseArray addObject:middleArray];
// [middleArray release];
i = -1;
count = 0;
}
}
return allChooseArray;
}
网友评论