鱼C论坛

 找回密码
 立即注册
查看: 1180|回复: 1

[已解决]为什么运行结果会多一个数字6?

[复制链接]
发表于 2023-9-1 15:16:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想按顺时针顺序依次输出矩阵中的元素:
[1,2,3,4],         
[5,6,7,8],     → [1,2,3,4,8,12,11,10,9,5,6,7]
[9,10,11,12]

代码:
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
right = len(matrix[0])
left = 0
bottom = len(matrix)
top = 0
result = []
while left < right and top < bottom:

    #从左到右遍历元素
    for i in range(left,right):
        result.append(matrix[top][i])

    #从上到下遍历元素
    for j in range(top+1,bottom):
        result.append(matrix[j][right-1])
    #从右到左遍历元素
    for x in range(right-2,left-1,-1):
        result.append(matrix[bottom-1][x])
    #从下到上
    for y in range(bottom-2,top,-1):
        result.append(matrix[y][left])
   
    #外层遍历结束后继续遍历内层
    left = left + 1
    right = right - 1
    bottom = bottom - 1
    top = top + 1
print(result)

输出结果
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, 6]
最佳答案
2023-9-1 15:16:44
问题出在代码的最后一行,即print(result)。在最后一次循环结束后,left、right、bottom和top的值都会发生变化,所以会多输出一个数字6。

在最后一次循环结束后,left = 1,right = 2,bottom = 2,top = 1。此时,由于left < right和top < bottom条件不满足,循环结束。但是在循环结束后,left = 2,right = 1,bottom = 1,top = 2。所以在最后一次循环中,result.append(matrix[y][left])这一行代码会被执行,将数字6添加到result列表中。

要解决这个问题,可以在print(result)之前添加一行代码来移除最后一个数字6,例如result.pop()。这样就可以得到正确的输出结果。

修改后的代码如下:

matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
right = len(matrix[0])
left = 0
bottom = len(matrix)
top = 0
result = []
while left < right and top < bottom:

    #从左到右遍历元素
    for i in range(left,right):
        result.append(matrix[top][ i])

    #从上到下遍历元素
    for j in range(top+1,bottom):
        result.append(matrix[j][right-1])
    #从右到左遍历元素
    for x in range(right-2,left-1,-1):
        result.append(matrix[bottom-1][x])
    #从下到上
    for y in range(bottom-2,top,-1):
        result.append(matrix[y][left])
   
    #外层遍历结束后继续遍历内层
    left = left + 1
    right = right - 1
    bottom = bottom - 1
    top = top + 1

result.pop()
print(result)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-1 15:16:44 | 显示全部楼层    本楼为最佳答案   
问题出在代码的最后一行,即print(result)。在最后一次循环结束后,left、right、bottom和top的值都会发生变化,所以会多输出一个数字6。

在最后一次循环结束后,left = 1,right = 2,bottom = 2,top = 1。此时,由于left < right和top < bottom条件不满足,循环结束。但是在循环结束后,left = 2,right = 1,bottom = 1,top = 2。所以在最后一次循环中,result.append(matrix[y][left])这一行代码会被执行,将数字6添加到result列表中。

要解决这个问题,可以在print(result)之前添加一行代码来移除最后一个数字6,例如result.pop()。这样就可以得到正确的输出结果。

修改后的代码如下:

matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
right = len(matrix[0])
left = 0
bottom = len(matrix)
top = 0
result = []
while left < right and top < bottom:

    #从左到右遍历元素
    for i in range(left,right):
        result.append(matrix[top][ i])

    #从上到下遍历元素
    for j in range(top+1,bottom):
        result.append(matrix[j][right-1])
    #从右到左遍历元素
    for x in range(right-2,left-1,-1):
        result.append(matrix[bottom-1][x])
    #从下到上
    for y in range(bottom-2,top,-1):
        result.append(matrix[y][left])
   
    #外层遍历结束后继续遍历内层
    left = left + 1
    right = right - 1
    bottom = bottom - 1
    top = top + 1

result.pop()
print(result)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 15:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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