python 中的优化方法。类似于排列
我有一种方法可以生成预订过程中的乘客数量。参数是最小和最大乘客数量。
只有 2 个条件:
number_of_adults
必须大于 0number_of_infants
必须小于或等于number_of_adults
这是代码:
def generate_numbers_of_paxes(min_number=1,max_number=9):
number_of_passengers=random.randint(min_number, max_number)
print(number_of_passengers)
number_of_adults=random.randint(min_number, number_of_passengers)
number_of_youths=random.randint(0, number_of_passengers-number_of_adults)
number_of_children=random.randint(0, number_of_passengers-number_of_adults-number_of_youths)
number_of_infants=random.randint(0, number_of_passengers-number_of_adults-number_of_youths-number_of_children)
number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
if(number_of_adults<number_of_infants):
difference=number_of_infants-number_of_adults
number_of_infants-=difference
number_of_adults+=difference
return [number_of_adults,number_of_youths,number_of_children,number_of_infants]
numbers_of_paxes=generate_numbers_of_paxes()
for (x,y) in enumerate(numbers_of_paxes):
print(x,y)
此代码效果很好,但正如您所看到的,行中存在一些问题
number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
,因为每个年龄段的人数总和并不总是等于 number_of_passegers
,并且我将差值添加到 number_of_adult
。
I have a method which generates me number of passengers to reservation process. The parameters are the minimum and maximum numbers of passengers.
There are only 2 conditions:
number_of_adults
must be greater than 0number_of_infants
must be less than or equal tonumber_of_adults
This is code:
def generate_numbers_of_paxes(min_number=1,max_number=9):
number_of_passengers=random.randint(min_number, max_number)
print(number_of_passengers)
number_of_adults=random.randint(min_number, number_of_passengers)
number_of_youths=random.randint(0, number_of_passengers-number_of_adults)
number_of_children=random.randint(0, number_of_passengers-number_of_adults-number_of_youths)
number_of_infants=random.randint(0, number_of_passengers-number_of_adults-number_of_youths-number_of_children)
number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
if(number_of_adults<number_of_infants):
difference=number_of_infants-number_of_adults
number_of_infants-=difference
number_of_adults+=difference
return [number_of_adults,number_of_youths,number_of_children,number_of_infants]
numbers_of_paxes=generate_numbers_of_paxes()
for (x,y) in enumerate(numbers_of_paxes):
print(x,y)
This code works good, but as you can see there is some problem in line
number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
because sum of number for every age group isn't always equals number_of_passegers
, and I add difference to number_of_adult
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
number_of_infants
计算应该很简单:否则,它很可能低于满足乘客要求所需的数量(
0
之间的随机
数字)以及乘客人数 - 成人人数 - 青少年人数 - 儿童人数
)。但是...您最好在成人之前计算婴儿数量,然后计算成人数量,并将
number_of_infants
作为random()
的最小值代码>,考虑到您列出的约束。将孩子的数量留到最后并应用相同的逻辑。由于这看起来像是家庭作业,因此我不会提供相关代码。也就是说:
生成随机数量的乘客
生成随机数量的婴儿
(不能超过数量的一半
乘客)
生成随机数量的成年人
(必须至少有婴儿数量)
生成随机数量的青少年
(不能多于乘客少
婴儿减去成人)
儿童数量是
乘客减去前三名
组。
另外,你为什么要这样做?
要么做:
要么:
Your
number_of_infants
calculation should be simply:Otherwise it is likely to be lower than the required number to meet your passenger requirements (a
random
number between0
andnumber_of_passengers - number_of_adults - number_of_youths - number_of_children
).However... you are better calculating the number of infants before adults and then calculating the number of adults with
number_of_infants
as the minimum for therandom()
, given the constraints you listed. Leave the number of children until last and apply the same logic. Since this looks like homework, I'll not provide the code for this.That is to say:
Generate random number of passengers
Generate random number of infants
(can't be more than half of number
of passengers)
Generate random number of adults
(must be at least number of infants)
Generate random number of youths
(can't be more than passengers less
infants less adults)
Number of children is number of
passengers less previous three
groups.
Also, why are you doing this?
Either do:
Or: