function verifyScheme(money
,count
,max
,min
)
{
if(min
> max
){
return '设置的最小砍价金额不能大于最大砍价金额'
}
if(count
* max
< money
){
return `设置的最大金额最少为:${money / count}`
}
if(count
* min
> money
){
return `设置的最小金额最多为:${money / count}`
}
return 1;
}
function cutPrice(min
,count
,price
){
return [price
/ count
,price
- (min
* (count
- 1))];
}
function cutPrice(money
,count
,max
,min
= 0.01)
{
let verRes
= verifyScheme(money
,count
,max
,min
)
if(verRes
!= 1){
throw new Error(verRes
)
}
let curPriceList
= [],nowMax
,nowMin
,nowPrice
;
money
*= 100;
max
*= 100;
min
*= 100;
for(let i
= 0 ; i
< count
;i
++)
{
nowMax
= money
-(count
- i
- 1) * min
;
if(nowMax
> max
){
nowMax
= max
;
}
nowMin
= money
- max
* (count
- i
- 1);
nowMin
= min
< nowMin
? nowMin
: min
;
nowPrice
= MathRondom(nowMax
,nowMin
);
curPriceList
.push(nowPrice
);
money
-= nowPrice
;
console
.log(`最大为:${nowMax/100},最小为${nowMin/100},【砍掉金额${nowPrice/100}】,剩余金额:${money/100},当前刀数${i+1},剩余刀数:${count-i-1}`)
}
return curPriceList
.map(item
=>item
/100);
}
function MathRondom(max
,min
)
{
return min
+ Math
.floor(Math
.random() * (max
- min
+ 1));
}
let cutPriceList
= cutPrice(99,10,20,5);
let count
= 0;
cutPriceList
.forEach(item
=>{
count
+= (item
*100)
})
console
.log(count
/100)
转载请注明原文地址: https://lol.8miu.com/read-22151.html