单个数求和后拆分 发表于 2017-07-07 | 输入一个数如38,拆分 3 + 8 = 11,1 + 1 = 2,最后2无法拆分就返回这是网上看到别人回来的,时间上差不多,代码也精简的多,完胜1234567- (NSUInteger)test:(NSUInteger)num{ while (num >= 10) { num = num/10 + num%10; } return num;} 先拆分求每位数累加的和,在将和去重复上一步操作,主要使用递归实现 1@property (nonatomic, assign) NSUInteger sum; 12345678910111213141516-(NSUInteger)splitNum:(NSUInteger)num{ self.sum += num % 10; if (num / 10 == 0) { if (self.sum / 10 != 0) { num = self.sum; self.sum = num % 10; }else{ return self.sum; } } return [self splitNum:num / 10];}