鱼C论坛

 找回密码
 立即注册
查看: 3027|回复: 2

一道python-字符串的题,OJ一直错误50%,但我找不到问题

[复制链接]
发表于 2020-2-26 00:09:18 | 显示全部楼层 |阅读模式
5鱼币
题目在图片里,我认为的细节勾画出来了,代码如下,在OJ里始终错误50%,纠结了一晚上,现在求助鱼油们啊啊啊啊啊
附上原题连接C语言网-字符串编辑题
  1. st=''
  2. while True:
  3.     st=input()
  4.     if '.'in t:
  5.         st+=t[:t.index('.')+1]   #如果有.则只取'.'以前(包含'.')的字符识=串
  6.         break
  7.     else:
  8.         st+=t
  9.         
  10. lis=list(st)
  11. commands=input()

  12. if commands[0]=='D':
  13.     if commands[-1] in st:
  14.         lis.remove(commands[-1])
  15.         print(''.join(lis))
  16.     else:
  17.         print('not find')
  18. elif commands[0]=='I':
  19.     old=commands[2]
  20.     if old in st:
  21.         new=commands[-1]
  22.         index=st.rfind(old)
  23.         lis.insert(index,new)
  24.         print(''.join(lis))
  25.     else:
  26.         print('not find')
  27. elif commands[0]=='R':
  28.     old=commands[2]
  29.     if old in st:
  30.         new=commands[-1]
  31.         length=len(lis)
  32.         while old in lis:
  33.             lis[lis.index(old)]=new
  34.         print(''.join(lis))
  35.     else:
  36.         print(''.join(lis))
复制代码

批注 2020-02-26 000547.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-26 00:58:09 | 显示全部楼层
  1. st=input()
  2. st=st.split('.')[0]+'.'
  3. commands=input()

  4. if commands[0]=='D':
  5.     if commands[-1] in st:
  6.         ret=st.replace(commands[-1],'',1)
  7.         print(ret)
  8.     else:
  9.         print('not find')
  10. elif commands[0]=='I':
  11.     old=commands[2]
  12.     if old in st:
  13.         new=commands[-1]
  14.         index=st.rfind(old)
  15.         ret=''.join([st[:index],new,st[index:]])
  16.         print(ret)
  17.     else:
  18.         print('not find')
  19. elif commands[0]=='R':
  20.     old=commands[2]
  21.     if old in st:
  22.         new=commands[-1]
  23.         ret=st.replace(old,new)
  24.         print(ret)
  25.     else:
  26.         print('not find')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-26 10:05:54 | 显示全部楼层

换一种形式是对的,昨晚我已经通过了,但是我想知道的是我的本文的代码问题出在哪里
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-22 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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