python练习五

1. 给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n),可知至少存在一个重复的整数,假设只有一个重复的整数,请找出这个重复的数

python 复制代码
def find_difnumber(ls):
    for index in range(0, len(ls)):
        for num in range(index + 1, len(ls)):
            if ls[index] == ls[num]:
                print(f"{ls[index]}这个数字出现了重复")
ls = [1,5,8,6,2,4,1,5,22,13,15,4]
print(ls)
find_difnumber(ls)

2. 找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)

python 复制代码
def find_number():
    for number in range(0, 10000):
        if number % 5 == 0 or number % 6 == 0:
            if number % 5 != number % 6:
                ls.append(number)
    print(ls)
ls = []
find_number()

3. 写一个方法,计算列表所有偶数下标元素的和(注意返回值)

python 复制代码
def count_even():
    count = 0
    for index in range(0, len(ls)):
        if ls[index] % 2 == 0:
            count += index
    print(count)
ls = [1,2,3,4,5,6,7,8,9,10]
count_even()

4. 【选做】某个人进入如下一个棋盘中,要求从左上角开始走,

最后从右下角出来(要求只能前进,不能后退),
问题:共有多少种走法?

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

python 复制代码
def count_paths(m, n):
    dp = [[0] * n for _ in range(m)]

    # 初始化第一行和第一列
    for i in range(m):
        dp[i][0] = 1
    for j in range(n):
        dp[0][j] = 1

    # 计算其他位置的路径数
    for i in range(1, m):
        for j in range(1, n):
            dp[i][j] = dp[i-1][j] + dp[i][j-1]

    return dp[m-1][n-1]

# 用户输入行数和列数
m = int(input("请输入棋盘的行数:"))
n = int(input("请输入棋盘的列数:"))

total_paths = count_paths(m, n)
print("从左上角到右下角的所有可能路径数为:", total_paths)

5. 【选做】汉诺塔:

python 复制代码
def hanoi(n, source, target, auxiliary):
    global move_count
    if n == 1:
        move_count += 1
        print(u"将圆盘 1 从 {} 移动到 {}".format(source, target))
        return
    hanoi(n - 1, source, auxiliary, target)
    move_count += 1
    print(u"将圆盘 {} 从 {} 移动到 {}".format(n, source, target))
    hanoi(n - 1, auxiliary, target, source)

# 初始化移动次数为0
move_count = 0

# 测试
num_disks = 4
hanoi(num_disks, 'A', 'C', 'B')
print("总共移动次数:", move_count)

我这里是4层

相关推荐
小宁爱Python15 分钟前
从零搭建 RAG 智能问答系统 6:Text2SQL 与工作流实现数据库查询
数据库·人工智能·python·django
m0_7482412322 分钟前
Java注解与反射实现日志与校验
java·开发语言·python
可触的未来,发芽的智生1 小时前
追根索源:换不同的词嵌入(词向量生成方式不同,但词与词关系接近),会出现什么结果?
javascript·人工智能·python·神经网络·自然语言处理
hu_nil1 小时前
LLMOps-第十一周作业
python·vllm
阿Q说代码2 小时前
IPIDEA实现数据采集自动化:高效自动化采集方案
运维·python·自动化·数据采集
大模型真好玩2 小时前
OCR技术简史: 从深度学习到大模型,最强OCR大模型花落谁家
人工智能·python·deepseek
AI量化投资实验室3 小时前
年化591%,回撤仅7%的策略,支持订阅信号|基于AgentScope开发金融多智能体,附python代码
开发语言·python·金融
CaracalTiger3 小时前
告别云端依赖!ComfyUI本地化视频生成实战教程+cpolar实战
python·gpt·开源·aigc·ai编程·1024程序员节·ai-native
追风少年ii3 小时前
脚本更新--CosMx、Xenium的邻域通讯分析(R版本)
linux·python·r语言·r·单细胞·培训
iteye_99393 小时前
Pycharm(社区办)安装(Window操作系统)
python