3个月熟练使用python--Day3打卡

3个月熟练使用python--Day3打卡

1、电影院买票问题

问题:2n个人排队进电影院,票价是50元。在这2n个人当中,其中n个人只有50元,另外n个人只有100元面钞。愚蠢的电影院开始卖票时1分钱也没有。问:有多少种排队方法使得每当一个拥有100元面钞的人买票时,电影院都有50元找钱?

思路:通过python自带的itertools库,罗列出n个50和n个100的所有排列组合情况,在根据排列组合中任何长度的50个数必须大于100的个数的条件,过滤掉不符合条件的组合,再去重计数;

代码:

import itertools

x=50

y=100

n=5

list_fifty=[]

list_hundred=[]

for i in range(1,n+1):

    list_fifty.append(x)

for i in range(1,n+1):

    list_hundred.append(y)

list_combine=list_fifty+list_hundred

list_givend=[]

list_last=[]

for p in itertools.permutations(list_combine,2*n):

    list_p=list(p)

    if p[0]==50:

        for i in range(0,len(list_p)):

            if list_p[0:i+1].count(50)<list_p[0:i+1].count(100):#每个位数递增看50的个数会不会比100的个数大,如50的个数比100的个数少,就break

                break

            elif list_p[0:i+1].count(50)>=list_p[0:i+1].count(100)and i==len(list_p)-1:#如50的个数大于等于100的个数,且循环到头了,就打印出来         

                list_givend.append(list_p) #把元素写入到数组中                 

                for element in list_givend:#元素去重

                    if element not in list_last:

                        list_last.append(element)

print('最终的排列组合有',len(list_last),'种,分别是',list_last)

如果大家对于学习Python有任何问题,学习方法,学习路线,如何学习有效率的问题,可以随时来咨询我,或者缺少系统学习资料的,我做这行年头比较久,自认为还是比较有经验的,可以帮助大家提出建设性建议,这是我的Python交流qun:785128166,有任何问题可以随时来咨询我。
 

总结:这道题困惑我很久,难点在于:

        ①、一开始没有用if break语句,导致有些不符合条件的排列组合也打印出来了;

        ②、打印出来的排列组合有重复的,要去重;

        总体来说,这个代码的效率不是很高,当我赋值n=5的时候,代码至少要跑几分钟,可能还有别的简单方法,希望大家的交流(* ̄︶ ̄)。

2、逻辑推理题

问题:

A、B、C、D、E五位同学各自从不同的途径打听到获得通讯赛第一名的同学的情况:

A:姓李,女同学,年龄13,广东人

B:姓张,男同学,年龄11,湖南人

C:姓陈,女同学,年龄13,广东人

D:姓黄,男同学,年龄11,广西人

E:姓张,男同学,年龄12,广东人

实际上,获得第一名的同学姓什么,性别,年龄,哪里人这四项情况在表中已有,但五位同学所打听到的情况,每人仅有一项是正确的。

请根据此推断获得第一名的同学的情况。

思路:通过对list列表中的元素赋值,在通过逻辑判断,算出来每个元素的值,再根据为1的元素对应的信息,来推算第一名同学的情况;

代码:

first=['姓李','女性','13岁','广东人']

second=['姓张','男性','11岁','湖南人']

third=['姓陈','女性','13岁','广东人']

fourth=['姓黄','男性','11岁','广西人']

fifth=['姓张','男性','12岁','广东人']

mix=first+second+third+fourth+fifth

#print(mix)

value=[0,1]

for first[0] in value:

    for first[1] in value:

        for first[2] in value:

            for first[3] in value:

                for second[0] in value:

                    for second[1] in value:

                        for second[2] in value:

                            for second[3] in value:

                                for third[0] in value:

                                    for third[1] in value:

                                        for third[2] in value:

                                            for third[3] in value:

                                                for fourth[0] in value:

                                                    for fourth[1] in value:

                                                        for fourth[2] in value:

                                                            for fourth[3] in value:

                                                                for fifth[0] in value:

                                                                    for fifth[1] in value:

                                                                        for fifth[2] in value:

                                                                            for fifth[3] in value:

                                                                                if first[0]+second[0]+third[0]+fourth[0]+fifth[0]>=1 and second[0]==fifth[0] and first[1]+second[1]+third[1]+fourth[1]+fifth[1]>=2 and first[1]==third[1] and second[1]==fourth[1]and fourth[1]==fifth[1] and first[1]!=second[1] and first[2]+second[2]+third[2]+fourth[2]+fifth[2]>=1 and first[2]==third[2] and second[2]==fourth[2]and first[3]+second[3]+third[3]+fourth[3]+fifth[3]>=1 and first[3]==third[3] and first[3]==fifth[3]and first[0]+first[1]+first[2]+first[3]==1 and second[0]+second[1]+second[2]+second[3]==1 and third[0]+third[1]+third[2]+third[3]==1 and fourth[0]+fourth[1]+fourth[2]+fourth[3]==1 and fifth[0]+fifth[1]+fifth[2]+fifth[3]==1:

                                                                                    result=first+second+third+fourth+fifth#列表合并

                                                                                    #print(result)

final=[]

for index,value in enumerate(result):#把为1的结果打印出来,打印出来的时候有重复,需要去重

    if value==1:

        final.append(mix[index])

final_result=list(set(final))#对列表进行去重

final_result.sort(key=final.index)

print(final_result)

总结:虽然题解出来了,但是我用了最笨的方法,用了16个for循环,程序上还有很大的优化空间;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值