其实这是在最近做商城的购物车时想到的,当有红包的时候怎么获取红包的最大数额问题 于是才有这个东西出现
NSMutableArray *redBags =[NSMutableArray arrayWithArray:@[@5,@15,@30,@40,@40,@50]]; //[NSMutableArray arrayWithArray:@[@5,@15,@25,@30,@50]];// 当前的红包
NSMutableArray *redBagsSelected = [NSMutableArray array];
for(int i = 0 ;i < 91;i++)
{
[redBagsSelected addObject:@0];
}
int price = 90;
char state[6][91];
for(int i = 0; i < redBags.count; i++)
{
for(int j = price; j >= [redBags[i] intValue]; j--)
{
int index = j- [redBags[i] intValue];
int tmp = [redBagsSelected[index] intValue]+ [redBags[i] intValue];
if(tmp > [redBagsSelected[j] intValue])
{
redBagsSelected[j] = [NSNumber numberWithInt:tmp];
state[i][j] = 1;
}
}
}
NSLog(@"给的数字为%d,最接近数字为%d",price,[redBagsSelected[price] intValue]);
for(int i = 0;i<6;i++)
{
NSLog(@"i == %s",state[i]);
for(int j = 0; j< 101;j++)
{
NSLog(@"j == %d",state[i][j]);
}
}
int i = (int)redBags.count;
int j = price;
NSLog(@"组合数字为:");
while(i-- >= 0)
{
if(state[i][j] == 1)
{
NSLog(@"%d ",[redBags[i] intValue]);
j -= [redBags[i] intValue];
}
}
还有就是获得不同的红包组合
网友评论