• oc 中组合排序算法


    - (NSMutableArray *)zuHeSuanFa:(NSMutableArray *)array chooseCount:(int)m
    {
        int n = (int)[array count];
        
        if (m > n)
        {
            return nil;
        }
        
    //    NSLog(@"从1到%d中取%d个数的组合。。。",n,m);
        
        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]];
    //            NSLog(@"%d ",i+1);
            }
        }
        
        [allChooseArray addObject:firstArray];
        //    [firstArray release];
    //    NSLog(@"============");
        
        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)
                    {
    //                    NSLog(@"%d ",k+1);
                        //                    [middleArray addObject:[NSString stringWithFormat:@"%d",k + 1]];
                        [middleArray addObject:[retArray objectAtIndex:k]];
                    }
                }
                
                [allChooseArray addObject:middleArray];
                //            [middleArray release];
                
    //            NSLog(@"============");
                
                i = -1;
                count = 0;
            }
        }
        
        return allChooseArray;
    }
  • 相关阅读:
    神舟笔记本反厂后带来的惊喜与郁闷
    如今是否还要坚持asp.net,坚持程序员这个不怎么光荣的称号
    严援朝的一句名言
    一个专科生程序员的痛苦境遇
    overflow:hidden 文本不在over 范围,也不显示
    困扰很久的问题
    未来已来,4K激活字库产业新世代
    4K超高清,为字库产业,打开了数字家电的大门
    2012中文字库简单统计与分类
    图说字王数格纵系列
  • 原文地址:https://www.cnblogs.com/qianLL/p/5613126.html
Copyright © 2020-2023  润新知