开始
–(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
int n=4;
int k=2;
if (k<=0||n<k) {
NSLog(@"输入错误");
return;
}
NSMutableArray *path=[NSMutableArray array];
NSMutableArray * result=[NSMutableArray array];
[self returnarraysandtotal:n tothek:k andbegin:1 andpath:path andres:result];
NSLog(@"%@",result);
}
-(void)returnarraysandtotal:(int)n tothek:(int)k andbegin:(int)begin andpath:(NSMutableArray *)path andres:(NSMutableArray *)result{
if (path.count==k) {
NSMutableArray *ss=[NSMutableArray arrayWithArray:path];
[result addObject:ss];
return;
}
for (int i=begin; i<=n; i++) {
[path addObject:[NSString stringWithFormat:@"%d",i]];
[self returnarraysandtotal:n tothek:k andbegin:i+1 andpath:path andres:result];
[path removeLastObject];
}
}