把一个大数拆成几个指定的小数相加,以下实现方式性能很差仅供参考:
- (NSMutableArray *)split:(NSInteger)number {
if (number < kThree) {
return nil;
}
if ((number-kThree)==0 || (number-kFour)==0 || (number-kFive)==0) {
NSMutableArray *_result = [NSMutableArray array];
NSMutableArray *_array = [NSMutableArray array];
[_array addObject:[NSNumber numberWithInt:number]];
[_result addObject:_array];
return _result;
}
NSInteger _left1 = number - kThree;
NSInteger _left2 = number - kFour;
NSInteger _left3 = number - kFive;
NSMutableArray *_total = [NSMutableArray array];
NSMutableArray *_rst1 = [self split:_left1];
if (!!_rst1 && (_rst1.count > 0)) {
for (NSMutableArray *_a in _rst1) {
[_a addObject:[NSNumber numberWithInt:kThree]];
}
[_total addObjectsFromArray:_rst1];
}
NSMutableArray *_rst2 = [self split:_left2];
if (!!_rst2 && (_rst2.count > 0)) {
for (NSMutableArray *_a in _rst2) {
[_a addObject:[NSNumber numberWithInt:kFour]];
}
[_total addObjectsFromArray:_rst2];
}
NSMutableArray *_rst3 = [self split:_left3];
if (!!_rst3 && (_rst3.count > 0)) {
for (NSMutableArray *_a in _rst3) {
[_a addObject:[NSNumber numberWithInt:kFive]];
}
[_total addObjectsFromArray:_rst3];
}
return _total;
}
拆数算法(ObjectiveC递归实现)
最新推荐文章于 2021-11-20 08:29:50 发布