python设计题60元按商品的比例平分_按比率分配的全部项目

使用双倍数学,四舍五入,然后根据比率随机分发剩余的水果。请注意,使用对象方向和循环可以使其不那么难看,但这只是一个开始。在public void distribute(int apple, int pear, int grape) {

double total = apple + pear + grape;

double appleRatio = apple/total;

double pearRatio = pear/total;

double grapeRatio = grape/total;

// apple worker

int appleWorkerApple = (int) (appleRatio*apple);

int appleWorkerPear = (int) (appleRatio*pear);

int appleWorkerGrape = (int) (appleRatio*grape);

// pear worker

int pearWorkerApple = (int) (pearRatio*apple);

int pearWorkerPear = (int) (pearRatio*pear);

int pearWorkerGrape = (int) (pearRatio*grape);

// grape worker

int grapeWorkerApple = (int) (grapeRatio*apple);

int grapeWorkerPear = (int) (grapeRatio*pear);

int grapeWorkerGrape = (int) (grapeRatio*grape);

int appleRemain = apple - appleWorkerApple - pearWorkerApple - grapeWorkerApple;

int pearRemain = pear - appleWorkerApple - pearWorkerApple - grapeWorkerApple;

int grapeRemain = grape - appleWorkerApple - pearWorkerApple - grapeWorkerApple;

Random r = new Random();

while(appleRemain > 0 && pearRemain > 0 && grapeRemain > 0) {

double target = r.nextDouble();

switch(r.nextInt(3)) {

case 0:

if(appleRemain > 0) {

appleRemain

if(target < appleRatio)

appleWorkerApple++;

else if (target < appleRatio + grapeRatio)

pearWorkerApple++;

else

grapeWorkerApple++;

}

break;

case 1:

if(grapeRemain > 0)

// etc.

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值