- n个数中取出m个的组合数( 例如:从1,2,3,4随机取出2个数,一共有6种)
-(int)getCountBets:(int)n selectCount:(int) m{
int minRange = m;
long num1 = 1;
for (int i = 1; i<=minRange; i++) {
num1 = num1 * i;
}
long num2 = 1;
for (int i = (int)n; I>n - minRange; i--) {
num2 = num2 * i;
}
int NewtotalZhus = (int)(num2/num1);
return NewtotalZhus;
}
2.表示从n个元素中取出m个元素的取法数(把所有的可能性列举出来)
- (void)combine:(int)n index:(int)k temp:(NSString *)str
{
for(int i = n; i >= k; i--)
{
if(k > 1)
{
// [self combine:i-1 index:k-1 temp:[NSString stringWithFormat:@"%@%@",str,[dataArr objectAtIndex:i-1]]];
[self combine:i-1 index:k-1 temp:[NSString stringWithFormat:@"%@,%d",str,i-1]];
} else
{
// NSLog(@"%@",[NSString stringWithFormat:@"排列可能性%@%@",str,[dataArr objectAtIndex:i-1]]);
NSString*oddsIndxStr;
oddsIndxStr = [NSString stringWithFormat:@"%@,%d",str,i-1];
[_oddsIndxArray addObject:oddsIndxStr];
NSLog(@"%@",oddsIndxStr);
}
}
}
调用
[self combine:(int)dataArr.count index:inx temp:@""];
网友评论