面试-字符串1

应用

第1个字符串:R

第2个字符串:BR

第3个字符串:RBBR

第4个字符串:BRRBRBBR

规律:第i个字符串 = 第i-1个字符串取反 + 第i-1个字符串,其中B、R互为相反字符。求第n个字符串的第k个字符为多少?n从1开始、k从0开始。

实现

python 复制代码
N = int(input("请输入第几个字符串:"))
K = int(input("请输入第几个字符:"))

first_string = "R"
current_chars = list(first_string)

#print(current_chars)
print(first_string)

def Nth_string(N, K, current_chars):
    
    next_chars = []

    if N == 1:
        # pass
        print(f"目标字符为:{current_chars[K]}")
    else:

      for i in range(0, len(current_chars)):
        
          if current_chars[i] == "R":
              next_chars.append("B")
          else:
              next_chars.append("R")
        
      for j in range(0, len(current_chars)):

          next_chars.append(current_chars[j])

      
      #print(next_chars)
      
      next_string = ""
      for k in next_chars:
          next_string = next_string + k
      print(next_string)

      Nth_string(N-1, K, next_chars)

    

Nth_string(N, K, current_chars)
powershell 复制代码
请输入第几个字符串:5
请输入第几个字符:3
R
BR
RBBR
BRRBRBBR
RBBRBRRBBRRBRBBR
目标字符为:R
相关推荐
happylifetree3 小时前
Python09:核心语法-数据存储与运算-字面量
python
心之语歌3 小时前
Tkinter 画布基本梳理
运维·服务器·python
Wx-bishekaifayuan4 小时前
django个性化旅游路线推荐平台49005-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
小白快快跑哦5 小时前
python-字符串全解(六):正则表达式-量词
python·正则表达式·字符串
禹凕5 小时前
机器学习之Selenium(Machina Learning about Selenium)
爬虫·python·selenium·测试工具·机器学习
ebiobiz7 小时前
基于 GD32 Embedded Builder (GEB) 与 Nimmake 的 MCU 工程搭建指南
c++·python·单片机·嵌入式硬件·mcu
老歌老听老掉牙7 小时前
穿越时空的物流密码:VRP与VRPTW的深度剖析及实战求解
python·物流·vrp
FYKJ_20107 小时前
django电影推荐系统55066-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
ShyanZh8 小时前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式
小静AI工程实验室8 小时前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法