蒋博文 发表于 2020-3-16 23:36:53

fan1993423 发表于 2020-3-16 19:33
可以的

最起码比我六批,我连题目该咋解读都还没懂,还不知道从哪下手

派生小生 发表于 2020-3-17 01:28:24

def f353(num_str):
    num_temp = []
    for num in num_str:
      if num.isdigit():
            num_temp.append(int(num))
            
    num_forever = num_temp.copy()
    a = num_temp.pop()
    check = False
    for i in range(len(num_temp)):
      if check:
            break
      b = num_temp
      num_temp.remove(b)
      count1 = count(a,b)
      c = num_temp
      d = num_temp
      count2 = count(c,d)
      for ab in count1:
            if check:
                break
            for cd in count2:
                if check:
                  break
                count3 = count(ab,cd)
                for abcd in count3:
                  if abcd == 24:
                        check = True
                        break
      if not check:
            num_temp.insert(i,b)
    if not check:
      print('False')
               
def count(a,b):
    count_list = []
    c = a+ b
    d = abs(a - b)
    e = a * b
    if b :
      f = a / b
    else:
      f = 0
    count_list.extend((c,d,e,f))
    return count_list


numlist = input("请输入4个数字:")
f353(numlist)

钙硼钡锗 发表于 2020-3-17 05:09:02

global a,aa,pp,b,e,ee
a=
b=[]
pp=0
e=[]
ee=[]

while True:
    random.shuffle(a)
    d=copy.copy(a)
    c=d
    d.pop(0)
   e=[]
   x(d,c)
   if pp==24:
   break

try:
    for i in range(len(a)):
    ee.append(a)
    ee.append(e)
except:
    None

def x(d,c):
    global pp
    global e
    if len(d)>0:
      p=random.randint(1,4)
      if p==1:
            e.append('+')
            x1(d,c)
      if p==2:
            e.append('-')
            x2(d,c)
      if p==3:
            e.append('*')
            x3(d,c)
      if p==4:
            e.append('/')
            x4(d,c)
    else:
      pp=c

def x1(d,c):
    c+=d
    d.pop(0)
    x(d,c)

def x2(d,c):
    c-=d
    d.pop(0)
    x(d,c)

def x3(d,c):
    c*=d
    d.pop(0)
    x(d,c)

def x4(d,c):
    c/=d
    d.pop(0)
    x(d,c)

def po():
    global u1,u2
    g1.delete(0,tk.END)
    g1.insert(0,u1)

wcshds 发表于 2020-3-17 09:18:52

from fractions import Fraction


def operation(num1, num2):
    operater = '+-*'
    result = []
    for i in operater:
      try:
            expression = eval(str(num1) + i + str(num2))
            result.append(expression)
      except:
            pass
    try:
      result.append(num2 - num1)
      result.append(Fraction(num1, num2))
      result.append(Fraction(num2, num1))
    except:
      pass
    return list(set(result))
      

def test(lista):
    for a in lista:
      temp1 = lista[:]
      temp1.remove(a)
      for b in temp1:
            res1 = operation(a, b)
            for c in res1:
                temp2 = lista[:]
                temp2.remove(a)
                temp2.remove(b)
                res2 = operation(temp2, temp2)
                for d in res2:
                  res3 = operation(c, d)
                  if 24 in res3:
                        return True
                for e in temp2:
                  res4 = operation(c, e)
                  for f in res4:
                        temp3 = lista[:]
                        temp3.remove(a)
                        temp3.remove(b)
                        temp3.remove(e)
                        for g in temp3:
                            res5 = operation(f, g)
                        if 24 in res5:
                            return True
    return False
到处修修补补,运行效率极低{:10_285:}

kinkon 发表于 2020-3-17 10:28:59

import math
def f353(arr):
    def solve(n):
      if(1 == n):
            if(math.fabs(24 - tempArr) < 1E-6):
                return True
            else:
                return False
      for i in range(0, n):
            for j in range(i+1, n):
                a, b = tempArr, tempArr               
                tempArr = tempArr
                tempArr = a + b
                if ( solve(n - 1) ) :
                  return True
                tempArr = a - b
                if solve(n - 1) :
                  return True
                tempArr = b - a
                if solve(n - 1):
                  return True
                tempArr = a * b
                if solve(n - 1):
                  return True;
                if b != 0:
                  tempArr = a / b
                  if solve(n - 1):
                        return True
                if a != 0:
                  tempArr = b / a
                  if solve(n - 1):
                        return True
                tempArr, tempArr = a, b      
      return False      
    tempArr = arr
    return True if solve(4) else False

whosyourdaddy 发表于 2020-3-17 11:54:32

import itertools
def func353(h):
    from itertools import permutations as p
    for a,b,c,d in p(list(map(str,h)),4):
      for x,y,z in p(list("+++---***///"),3):
            if eval((str(eval(a+x+b))+y+c+z+d))==24:
                return True
            if eval(str(eval(a+x+b))+y+str(eval(c+z+d)))==24:
                return True
            if eval(str(eval(str(eval(a+x+b))+y+c))+z+d)==24:
                  return True
    return False

776667 发表于 2020-3-17 12:16:02

from itertools import permutations

def fun353(x):
    for cards in permutations(x):
      for i in ['+','-','*','/']:
            for j in ['+','-','*','/']:
                for n in ['+','-','*','/']:
                  if eval('%s%s%s%s%s%s%s'%(cards,i,cards,j,cards,n,cards)) == 24:
                        return True
      cards_1 = cards
      cards_2 = cards
      for i in ['+','-']:
            for j in ['+','-']:
                sum_cards_2 = eval('%s%s%s%s%s'%(cards_2,i,cards_2,j,cards_2))
                try:
                  if cards_1*sum_cards_2==24 or cards_1/sum_cards_2==24:
                        return True
                except ZeroDivisionError:
                  continue
      cards_2 = cards
      cards_1 = cards[:3]
      for i in ['+','-']:
            for j in ['+','-']:
                sum_cards_1 = eval('%s%s%s%s%s'%(cards_1,i,cards_1,j,cards_1))
                try:
                  if sum_cards_1*cards_2==24 or sum_cards_1/cards_2==24:
                        return True
                except ZeroDivisionError:
                  continue
      cards_1 = cards[:2]
      cards_2 = cards
      for i in ['+','-']:
            for j in ['+','-']:
                sum_cards_1 = eval('%s%s%s'%(cards_1,i,cards_1))
                sum_cards_2 = eval('%s%s%s'%(cards_2,i,cards_2))
                try:
                  if sum_cards_1*sum_cards_2==24 or sum_cards_1*sum_cards_2==24:
                        return True
                except ZeroDivisionError:
                  continue
    return False

zltzlt 发表于 2020-3-17 13:21:56

l0stparadise 发表于 2020-3-16 19:32
感觉自己的方法好蠢。。。。

解答错误

输入:
输出:False
预期结果:True

zltzlt 发表于 2020-3-17 13:23:00

塔利班 发表于 2020-3-16 20:02


解答错误

输入:
输出:True
预期结果:False

zltzlt 发表于 2020-3-17 13:24:55

派生小生 发表于 2020-3-17 01:28


解答错误

输入:
输出:False
预期结果:True

zltzlt 发表于 2020-3-17 13:25:44

钙硼钡锗 发表于 2020-3-17 05:09
global a,aa,pp,b,e,ee
a=
b=[]


缩进改一下哈

zltzlt 发表于 2020-3-17 13:26:16

wcshds 发表于 2020-3-17 09:18
到处修修补补,运行效率极低

1204 ms

zltzlt 发表于 2020-3-17 13:27:08

kinkon 发表于 2020-3-17 10:28


68 ms

zltzlt 发表于 2020-3-17 13:27:36

whosyourdaddy 发表于 2020-3-17 11:54
import itertools
def func353(h):
    from itertools import permutations as p


输入 出错

zltzlt 发表于 2020-3-17 13:28:07

776667 发表于 2020-3-17 12:16


解答错误

输入:
输出:False
预期结果:True

塔利班 发表于 2020-3-17 13:46:18

zltzlt 发表于 2020-3-17 13:23
解答错误

输入:


def f353(x):
    from itertools import combinations as cb
    def com(a,b):
      l=len(a)
      if l==1:
            return a==b
      m=eval('*'.join(map(str,a)))
      if m==b:
            return True
      elif m<b:
            return False
      elif l==2:
            e,f=a
            return com(,b+f) or com(,b-f) or com(,b*f) or com(,b/f)
      else:
            for e,f in cb(a,2):
                if f<e:
                  e,f=f,e
                t=a[:]
                t.remove(e)
                t.remove(f)
                for g in :
                  if com(t,b+g) or com(t,b-g) or com(t,b*g) or com(t,b/g) if g else 0:
                        return True
            return False
    return com(x,24)
恩,再看看- -

whosyourdaddy 发表于 2020-3-17 17:54:37

import itertools
def func353(h):
    from itertools import permutations as p
    for a,b,c,d in p(list(map(str,h)),4):
      for x,y,z in p(list("+++---***///"),3):
            try:
                if eval((str(eval(a+x+b))+y+c+z+d))==24:
                  return True
                if eval(str(eval(a+x+b))+y+str(eval(c+z+d)))==24:
                  return True
                if eval(str(eval(str(eval(a+x+b))+y+c))+z+d)==24:
                  return True
            except ZeroDivisionError:
                continue
    return False

eachill 发表于 2020-3-19 17:35:40

from operator import add,sub,mul,truediv
import itertools
operators = (add, sub, mul, truediv)
sucess = []
def n353(list1):

    for h in itertools.permutations(list1, 4):
      for i in operators:
            for j in operators:
                for k in operators:
                  try:
                        result = i(j(k(h, h), h), h)
                        if result == 24:
                            return True
                  except:
                        pass
                  try:
                        result = i(j(h, h), k(h, h))
                        if result == 24:
                            return True
                  except:
                        pass
    return False
页: 1 [2]
查看完整版本: Python:每日一题 353