跳到主要內容

求出n取k組合的列表 Objective-C 版

這是本灌水系列最後一篇,到此已練功完成。
如果有其他語言需要解答,我找到這裡有各種不同的程式語言版本(缺Objective-C XD);此站看來是學生作業的好幫手呀。

以下是特別版的code:
#import <Foundation/Foundation.h>

@interface Combinations : NSObject
{
    NSMutableArray *list;
}

-(NSMutableArray *) calc: (int) all andWant: (int) want;
-(void) calc: (NSMutableArray *) before andAll: (int) all andWant: (int) want;
-(void) addList:(NSMutableArray *) before andAfter: (NSMutableArray *) after;
@end

@implementation Combinations

-(void) addList:(NSMutableArray *)before andAfter:(NSMutableArray *)after {
    NSMutableArray *temp= [NSMutableArray arrayWithArray:before];
    [temp addObjectsFromArray:after];
    [list addObject:temp];
}

-(void) calc:(NSMutableArray *) before andAll: (int) all andWant:(int)want {
    NSMutableArray *temp= [[NSMutableArray alloc] init];
    if(want == 0)
    {
        for(int i=0; i<all;i++)
        {
            [temp addObject:  @"0"];
        }
        [self addList: before andAfter: temp];
    }
    else if(all == want) {
        for(int i=0; i<all;i++)
        {
            [temp addObject: @"1"];
        }
        
        [self addList: before andAfter: temp];
    }
    else if(all == 1)
    {
        switch(want)
        {
                NSString *k =  [NSString stringWithFormat:@"%d", want];
                [temp addObject:k];
        }
        [self addList: before andAfter: temp];
    }
    else
    {
        NSMutableArray *newbefore=[NSMutableArray
                                   arrayWithArray:before];
        
        [newbefore addObject:@"0"];
        [self calc:  newbefore andAll: all-1 andWant: want];
        
        NSMutableArray *newbefore2=[NSMutableArray
                                    arrayWithArray:before];
        [newbefore2 addObject:@"1"];
        [self calc:  newbefore2 andAll: all-1 andWant: want-1];
    }
}

-(NSMutableArray *) calc :(int)all andWant :(int)want {
    list =[[NSMutableArray alloc]init];
    NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:
                            nil];
    [self calc:temp andAll:all andWant:want];
    return list;
}
@end

int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Combinations *cb = [Combinations alloc];
    int all=4;
    int want=2;
    if(argc == 3)
    {
        all = atoi(argv[1]);
        want = atoi(argv[2]);
    }
    NSMutableArray *arr=[cb calc :all andWant:want];
    printf("\n%d取%d,結果共%lu個\n", all, want, arr.count);
    for(int i=0; i<arr.count; i++) {
        NSMutableArray *temp =[arr objectAtIndex:i];
        for(int j=0; j<temp.count; j++) {
            printf("%s ",  [[temp objectAtIndex:j] UTF8String]);
        }
        puts("");
    }
    [cb release];
    [pool drain];
    return 0;
}

以下是一般的組合版本
#import <Foundation/Foundation.h>
#import <Foundation/NSArray.h>

@interface Combinations : NSObject
{
    NSMutableArray *list;
}

-(NSMutableArray *) calc: (int) all andWant: (int) want;
-(void)calc: (NSMutableArray *) before andInlist:(NSMutableArray *) inlist andAll: (int) all andWant: (int) want;

-(void) addList:(NSMutableArray *) thelist ;
-(void) addList:(NSMutableArray *) before andAfter: (NSMutableArray *) after;
@end

@implementation Combinations

-(void) addList:(NSMutableArray *) thelist{
    [list addObject:thelist];
}

-(void) addList:(NSMutableArray *)before andAfter:(NSMutableArray *)after {
    NSMutableArray *temp= [NSMutableArray arrayWithArray:before];
    [temp addObjectsFromArray:after];
    [list addObject:temp];
}

-(void) calc:(NSMutableArray *) before andInlist: (NSMutableArray *) inlist andAll: (int)all andWant:(int)want {
    NSMutableArray *temp= [[NSMutableArray alloc] init];
    if(want == 0) {
        [self addList:before];
    } else if(all == want) {
        [self addList:before andAfter:inlist];
        
    }
    else
    {
        NSMutableArray *newarr= [NSMutableArray arrayWithArray:before];
        temp= [NSMutableArray arrayWithArray:inlist];
        [temp removeObjectAtIndex:0];
        [newarr addObject: [inlist objectAtIndex: 0]];
        [self calc:newarr andInlist:temp andAll:all-1 andWant:want-1];
        [self calc:before andInlist:temp andAll:all-1 andWant:want];
    }
}

-(NSMutableArray *) calc:(int)all andWant:(int)want {
    list =[[NSMutableArray alloc]init];
    NSMutableArray *alllist = [[NSMutableArray alloc] init];
    NSMutableArray *empty = [[NSMutableArray alloc] init];
    for(int i=1; i<=all; i++) {
        [alllist addObject: [NSString stringWithFormat:@"%d", i]];
    }
    [self calc:empty andInlist:alllist andAll:all andWant:want];
    return list;
}
@end

int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Combinations *cb = [Combinations alloc];
    int all=4;
    int want=2;
    if(argc == 3)
    {
        all = atoi(argv[1]);
        want = atoi(argv[2]);
    }
    NSMutableArray *arr=[cb calc:all andWant:want];
    printf("\n%d取%d,結果共%lu個\n", all, want, arr.count);
    for(int i=0; i<arr.count; i++) {
        NSMutableArray *temp =[arr objectAtIndex:i];
        for(int j=0; j<temp.count; j++) {
            printf("%s ",  [[temp objectAtIndex:j] UTF8String]);
        }
        puts("");
    }
    [pool drain];
    return 0;
}

留言

這個網誌中的熱門文章

自然人憑證讀卡機驅動程式

鳥毅用的是第一代的自然人憑證讀卡機,EZ100PU(後來有同事買EZmini可以讀SIM卡似乎更好),每年報稅時用一次。 本來只是要申請些政府業務,一時之間找不到光碟,沒想到在 驅動程式下載 居然看到Linux和Mac的驅動程式,剩下的就是政府單位的網頁和程式應該改版了吧!!!

DBeaver 介面語言

DBeaver是我個人頗常用的一套跨平台Database管理工具,最近升級後發現Windows版本居然變成簡體中文,而且無法切換為英文。

如何將較高版本SQL Server複製到低版本SQL Server (降級為舊版)並保留權限及資料庫圖表

一般若是要將SQL Server裡的Database轉往其他Server時,最簡單的方式就是備份(Backup)後再還原(Restore),或者是䣃離(detach)後附加(attach)。 但是很不幸地,若是由較低版本(e.g. 2008)到較高版本(e.g. 2012)要怎麼辦呢?