假設我們這里有個正整數18,這里需要把18拆分成1 2 3 5 10的組合,那么輸出的結果應該是:
==> 10+5+3
eg:->79 ----->7*10+5+3+1 大數優先
類似這樣的效果,這里寫了一個簡單的算法來實現
SModel.h
#import <Foundation/Foundation.h>
@interface SModel : NSObject
@property (nonatomic,assign) NSInteger totalCount; //總個數
@property (nonatomic,assign) NSInteger count10; //10的個數
@property (nonatomic,assign) NSInteger count5; //5的個數
@property (nonatomic,assign) NSInteger count3; //3的個數
@property (nonatomic,assign) NSInteger count2; //2的個數
@property (nonatomic,assign) NSInteger count1; //1的個數
@property (nonatomic,strong) NSMutableArray *sArr;
@end
拆分方法
-(SModel*)splitScore:(NSInteger)score{
NSInteger y = 0; //余數
SModel *model = [[SModel alloc] init];
do {
if (score >= 10) {
y = score%10;
score = score/10;
NSLog(@"%zix10",score);
model.totalCount+=score;
model.count10 = score;
score = y;
}else if(score >= 5){
y = score%5;
score = score/5;
NSLog(@"%zix5",score);
model.totalCount+=score;
model.count5 = score;
score = y;
}else if(score >= 3){
y = score%3;
score = score/3;
NSLog(@"%zix3",score);
model.totalCount+=score;
model.count3 = score;
score = y;
}else {
NSLog(@"%zi",score);
model.count1 = score;
model.totalCount+=score;
score = y;
break;
}
}while (1);
return model;
}
計算完成之后通過一個Model把數據存儲起來,包括總的拆分的個數,1 2 3 5 10每個數字的個數,最后拿到模型就可以去處理相應的邏輯
類似這樣的功能和紙牌游戲中的加注功能類似