题解 | #二维数组操作#
二维数组操作
https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4?tpId=37&tqId=21306&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D2%26judgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37%26dayCountBigMember%3D90%25E5%25A4%25A9&difficulty=2&judgeStatus=3&tags=&title=
就看了一圈,别的题解主要以实现功能为主。那我就写一份稍微规范一些的代码吧0.0
class TwoDArr:
def __init__(self, m, n):
if m > 9 or n > 9:
return print(-1)
self.arr = [[0]*n for _ in range(m)]
return print(0)
def ExchangeElm(self, x1, y1, x2, y2):
if x1 < 0 or x1 >= len(self.arr) or y1 < 0 or y1 >= len(self.arr[0]):
return -1
if x2 < 0 or x2 >= len(self.arr) or y2 < 0 or y2 >= len(self.arr[0]):
return -1
self.arr[x1][y1] = self.arr[x2][y2]
return 0
def AddRow(self, x):
if len(self.arr) >= 9 or x < 0 or x > len(self.arr)-1:
return -1
self.arr.insert(x-1, [0]*len(self.arr[0]))
self.arr.pop()
return 0
def AddCol(self, y):
if len(self.arr[0]) >= 9 or y < 0 or y > len(self.arr[0])-1:
return -1
for i in range(len(self.arr)):
self.arr[i].insert(y-1, 0)
self.arr[i].pop()
return 0
def GetElem(self, x, y):
if x < 0 or x >= len(self.arr) or y < 0 or y >= len(self.arr[0]):
return -1
return self.arr[x][y]
while True:
try:
m, n = map(int, input().split()) # 初始化
x1, y1, x2, y2 = map(int, input().split()) # 交换
r = int(input()) # 行插入
c = int(input()) # 列插入
x, y = map(int, input().split()) # 查询元素
Solution = TwoDArr(m, n)
print(Solution.ExchangeElm(x1, y1, x2, y2))
print(Solution.AddRow(r))
print(Solution.AddCol(c))
print(Solution.GetElem(x, y))
except:
break
