weixin_42023040
weixin_42023040
采纳率100%
2018-05-16 06:02 阅读 2.0k

猜数字python代码,画流程图,关键地方的流程图即可

80

guess=0
a1=0
a2=1
a3=2
a4=3
a=[a1,a2,a3,a4]
b1=0
b2=1
b3=2
b4=3
b=[b1,b2,b3,b4]
def A(guess):
A=0
if a1==b1:
A=A+1
if a2==b2:
A=A+1
if a3==b3:
A=A+1
if a4==b4:
A=A+1
return(A)

def B(guess):
B=0
for i in range(0,4):
for k in range(0,4):
if a[i]==b[k]:
B=B+1
return(B)

e=[i for i in range(1,10000)]
for i in range(0,9999):
b1=int(e[(9998-i)]/1000)
b2=int((e[(9998-i)]-1000*b1)/100)
b3=int((e[(9998-i)]-1000*b1-100*b2)/10)
b4=int(e[(9998-i)]-1000*b1-100*b2-10*b3)
if b1==b2 or b1==b3 or b1==b4 or b2==b3 or b2==b4 or b3==b4:
del(e[9998-i])
f=len(e)

def number(e):
q=10000
p=0
f=len(e)
for g in range(0,f):
guess=e[g]
a1=int(e[g]/1000)
a2=int((e[g]-1000*a1)/100)
a3=int((e[g]-1000*a1-100*a2)/10)
a4=int(e[g]-1000*a1-100*a2-10*a3)
a=[a1,a2,a3,a4]
j=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for h in range(0,f):
b1=int(e[h]/1000)
b2=int((e[h]-1000*b1)/100)
b3=int((e[h]-1000*b1-100*b2)/10)
b4=int(e[h]-1000*b1-100*b2-10*b3)
b=[b1,b2,b3,b4]
j[int((B(guess)+1)*B(guess)/2+A(guess))]=j[int((B(guess)+1)*B(guess)/2+A(guess))]+1
if q>max(j):
q=max(j)
p=e[g]
return(p)

for i in range(1,8):
if i ==1:
print('第',i,'轮')
print('电脑给出的数字是')
print(e[0])
c=int(input('数字与位置都猜对的数量'))
d=int(input('猜对数字的数量'))
if (c==4):
break
a1=int(e[0]/1000)
a2=int((e[0]-1000*a1)/100)
a3=int((e[0]-1000*a1-100*a2)/10)
a4=int(e[0]-1000*a1-100*a2-10*a3)
a=[a1,a2,a3,a4]
for k in range(0,f):
guess=e[f-1-k]
b1=int(guess/1000)
b2=int((guess-1000*b1)/100)
b3=int((guess-1000*b1-100*b2)/10)
b4=int(guess-1000*b1-100*b2-10*b3)
b=[b1,b2,b3,b4]
if A(guess)!=c or B(guess)!=d:
del(e[f-1-k])
f=len(e)
if i>1:

    print('第',i,'轮')
    print('电脑给出的数字是')
    print(number(e))
    c=int(input('数字与位置都猜对的数量'))
    d=int(input('猜对数字的数量'))
    if (c==4):
        break
    a1=int(number(e)/1000)
    a2=int((number(e)-1000*a1)/100)
    a3=int((number(e)-1000*a1-100*a2)/10)
    a4=int(number(e)-1000*a1-100*a2-10*a3)
    a=[a1,a2,a3,a4]
    for k in range(0,f):
        guess=e[f-1-k]
        b1=int(guess/1000)
        b2=int((guess-1000*b1)/100)
        b3=int((guess-1000*b1-100*b2)/10)
        b4=int(guess-1000*b1-100*b2-10*b3)
        b=[b1,b2,b3,b4]
        if A(guess)!=c or B(guess)!=d:
            del(e[f-1-k])

    f=len(e)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    weixin_42023040 weixin_42023040 2019-01-10 12:37

    guess=0
    a1=0
    a2=1
    a3=2
    a4=3
    a=[a1,a2,a3,a4]
    b1=0
    b2=1
    b3=2
    b4=3
    b=[b1,b2,b3,b4]
    def A(guess):
    A=0
    if a1==b1:
    A=A+1
    if a2==b2:
    A=A+1
    if a3==b3:
    A=A+1
    if a4==b4:
    A=A+1
    return(A)

    def B(guess):
    B=0
    for i in range(0,4):
    for k in range(0,4):
    if a[i]==b[k]:
    B=B+1
    return(B)

    e=[i for i in range(1,10000)]
    for i in range(0,9999):
    b1=int(e[(9998-i)]/1000)
    b2=int((e[(9998-i)]-1000*b1)/100)
    b3=int((e[(9998-i)]-1000*b1-100*b2)/10)
    b4=int(e[(9998-i)]-1000*b1-100*b2-10*b3)
    if b1==b2 or b1==b3 or b1==b4 or b2==b3 or b2==b4 or b3==b4:
    del(e[9998-i])
    f=len(e)

    def number(e):
    q=10000
    p=0
    f=len(e)
    for g in range(0,f):
    guess=e[g]
    a1=int(e[g]/1000)
    a2=int((e[g]-1000*a1)/100)
    a3=int((e[g]-1000*a1-100*a2)/10)
    a4=int(e[g]-1000*a1-100*a2-10*a3)
    a=[a1,a2,a3,a4]
    j=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for h in range(0,f):
    b1=int(e[h]/1000)
    b2=int((e[h]-1000*b1)/100)
    b3=int((e[h]-1000*b1-100*b2)/10)
    b4=int(e[h]-1000*b1-100*b2-10*b3)
    b=[b1,b2,b3,b4]
    j[int((B(guess)+1)*B(guess)/2+A(guess))]=j[int((B(guess)+1)*B(guess)/2+A(guess))]+1
    if q>max(j):
    q=max(j)
    p=e[g]
    return(p)

    for i in range(1,8):
    if i ==1:
    print('第',i,'轮')
    print('电脑给出的数字是')
    print(e[0])
    c=int(input('数字与位置都猜对的数量'))
    d=int(input('猜对数字的数量'))
    if (c==4):
    break
    a1=int(e[0]/1000)
    a2=int((e[0]-1000*a1)/100)
    a3=int((e[0]-1000*a1-100*a2)/10)
    a4=int(e[0]-1000*a1-100*a2-10*a3)
    a=[a1,a2,a3,a4]
    for k in range(0,f):
    guess=e[f-1-k]
    b1=int(guess/1000)
    b2=int((guess-1000*b1)/100)
    b3=int((guess-1000*b1-100*b2)/10)
    b4=int(guess-1000*b1-100*b2-10*b3)
    b=[b1,b2,b3,b4]
    if A(guess)!=c or B(guess)!=d:
    del(e[f-1-k])
    f=len(e)
    if i>1:

    点赞 评论 复制链接分享
  • EmotionAC 3个T 2018-05-16 06:24
    点赞 评论 复制链接分享
  • lgyt240054 EuclideanSpace 2018-05-16 07:45

    人出机猜?而不是机出人猜?
    机出人猜的,这里了解一下https://blog.csdn.net/killua_hzl/article/details/5789646
    用的是2.的python版本
    我自己用3.6版本实现了,需要改的也就把print加个括号和第54行改为if not ch.digits:就行了
    最主要的函数还是numJudge,通过用户输入和机器生成的数字做对比,来实现AB的判断图片说明

    点赞 评论 复制链接分享
  • lgyt240054 EuclideanSpace 2018-05-16 07:48

    然后自己就玩上了,哈哈图片说明

    点赞 评论 复制链接分享

相关推荐