鱼C论坛

 找回密码
 立即注册
楼主: zltzlt

[已解决]Python:每日一题 323

[复制链接]
 楼主| 发表于 2020-2-5 22:01:40 | 显示全部楼层
546623863 发表于 2020-2-5 21:59
傻逼了,之前改的忘记改回去了

解答错误

输入:"211738"
输出:False
打印:
2 1
2 11
21 1
预期结果:True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-5 22:06:32 | 显示全部楼层
zltzlt 发表于 2020-2-5 22:01
解答错误

输入:"211738"
  1. def fun323(num):
  2.     length = len(num)
  3.     if length < 3:
  4.         return False
  5.     for i in range(length//2):
  6.         if num[0] == '0' and i >= 1:
  7.                 continue
  8.         for j in range(i+1,(length+1)//2+1):
  9.             if num[i+1] == '0' and j != i+1:
  10.                 continue
  11.             a = int(num[:i+1])
  12.             b = int(num[i+1:j+1])
  13.             nextindex = j+1
  14.             nextc = a+b
  15.             lenitem = len(str(nextc))
  16.             while nextindex < length and nextindex+lenitem <= length and int(num[nextindex:nextindex+lenitem]) == nextc:
  17.                 a = b
  18.                 b = int(num[nextindex: nextindex + lenitem])
  19.                 nextc = a+b
  20.                 nextindex += lenitem
  21.                 lenitem = len(str(nextc))
  22.             if nextindex == length:
  23.                 return True
  24.     return False
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-5 22:07:55 | 显示全部楼层

解答错误

输入:"111"
输出:True
预期结果:False
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-5 22:11:08 | 显示全部楼层

解答错误

输入:"112358"
输出:False
预期结果:True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-5 22:14:22 | 显示全部楼层
本帖最后由 archlzy 于 2020-2-5 22:24 编辑
  1. def fun323(string):
  2.     l_s = len(string)
  3.     l = (l_s-1) // 2
  4.     for i in range(1,l+1):
  5.         for j in range(i+1,l_s):
  6.             w1 = string[0:i]
  7.             w2 = string[i:j]
  8.             if (w1[0] =='0'and len(w1) != 1) or (w2[0] =='0'and len(w2) != 1):
  9.                 continue
  10.             temp_str = w1 + w2
  11.             while len(temp_str) < l_s:
  12.                 w3 = str(int(w1)+int(w2))
  13.                 temp_str += w3
  14.                 w1 = w2
  15.                 w2 = w3
  16.             if temp_str == string:
  17.                 return True
  18.     return False
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-5 22:24:11 | 显示全部楼层
zltzlt 发表于 2020-2-5 22:07
解答错误

输入:"111"
  1. def fun323(num):
  2.     length = len(num)
  3.     if length < 3:
  4.         return False
  5.     for i in range(length//2):
  6.         if num[0] == '0' and i >= 1:
  7.                 continue
  8.         for j in range(i+1,length//2+1):
  9.             if num[i+1] == '0' and j != i+1:
  10.                 continue
  11.             a = int(num[:i+1])
  12.             b = int(num[i+1:j+1])
  13.             nextindex = j+1
  14.             nextc = a+b
  15.             print(a,b)
  16.             lenitem = len(str(nextc))
  17.             while nextindex < length and nextindex+lenitem <= length and int(num[nextindex:nextindex+lenitem]) == nextc:
  18.                 a = b
  19.                 b = int(num[nextindex: nextindex + lenitem])
  20.                 nextc = a+b
  21.                 nextindex += lenitem
  22.                 lenitem = len(str(nextc))
  23.             if nextindex == length:
  24.                 return True
  25.     return False
复制代码


这次应该没问题了吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-5 23:08:19 | 显示全部楼层
zltzlt 发表于 2020-2-5 22:11
解答错误

输入:"112358"

def solve323(str1):
    def get(a, b, c):
        each = int(a) + int(b)
        if len(a) != 1 and a[0] == '0' or b[0] == '0' and len(b) != 1 or c[0] == '0' and len(c) != 1:
            return False
        if each == int(c):
            return True
        elif c.startswith(str(each)):
            return get(b,str(each),c[len(str(each)):])
        else:
            return False
    if len(str1) < 3:
        return False
    for i in range(1,len(str1)-1):
        for j in range(i+1,len(str1)):
            a,b,c = str1[:i],str1[i:j],str1[j:]
            if get(a,b,c):
                return True
    return False
print(solve323("199100199"))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-5 23:12:44 | 显示全部楼层
fan1993423 发表于 2020-2-5 21:19
哈哈,我俩思路基本一致,就是 a[0]=='0',不是数字0,忘了打引号了吧。

我最开始的思路是先遍历寻找前两个数字,然后通过这俩来往后递推,,然后发现198019823962
这个测试点过不去,,接下来才修改为这个方法,能避免这个错误!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-18 15:42:45 | 显示全部楼层
一个账号 发表于 2020-2-4 19:11
悬赏的题是你不会做吧......

真相了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-21 10:58:45 | 显示全部楼层
  1. def fun323(n):

  2.     def funtion_x(n,p_1,p_2,p_3):
  3.         num_1 = n[p_1:p_2]
  4.         num_2 = n[p_2:p_3]
  5.         sum_num = int(num_1) + int(num_2)
  6.         p_4 = p_3 + len(str(sum_num))
  7.         try:
  8.             num_3 = n[p_3:p_4]
  9.         except IndexError:
  10.             return False
  11.         for num in [num_1,num_2,num_3]:
  12.             if len(num) > 1 and num[0] == '0':
  13.                 return False
  14.         if sum_num == int(num_3):
  15.             if p_4 == len(n):
  16.                 return True
  17.             else:
  18.                 p_1 = p_2
  19.                 p_2 = p_3
  20.                 p_3 = p_4
  21.                 return funtion_x(n,p_1,p_2,p_3)
  22.         else:
  23.             return False
  24.         
  25.     for i in range(1,(len(n)//2)+1):
  26.         for j in range(i+1,len(n)):
  27.             p_1 = 0
  28.             p_2 = i
  29.             p_3 = j
  30.             if funtion_x(n,p_1,p_2,p_3):
  31.                 return True
  32.     return False
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 16:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表