首页 > 试题广场 >

二维数组操作

[编程题]二维数组操作
  • 热度指数:100179 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个大小的数据表,你会依次进行以下5种操作:
1.输入,初始化大小的表格。
2.输入x_1y_1x_2y_2,交换坐标在(x_1,y_1)(x_2,y_2)的两个数。
3.输入,在第上方添加一行。
4.输入,在第左边添加一列。
5.输入,查找坐标为的单元格的值。

请编写程序,判断对表格的各种操作是否合法。

详细要求:

1.数据表的最大规格为9行*9列,对表格进行操作时遇到超出规格应该返回错误。
2.对于插入操作,如果插入后行数或列数超过9了则应返回错误。如果插入成功了则将数据表恢复至初始化的大小,多出的数据则应舍弃。

3.所有输入坐标操作,对大小的表格,行号坐标只允许0~m-1,列号坐标只允许0~n-1。超出范围应该返回错误。

本题含有多组样例输入!行列从0开始标号
数据范围:数据组数:
进阶:时间复杂度:,空间复杂度:

输入描述:

输入数据按下列顺序输入:
1 表格的行列值
2 要交换的两个单元格的行列值
3 输入要插入的行的数值
4 输入要插入的列的数值
5 输入要查询的单元格的坐标



输出描述:

输出按下列顺序输出:
1 初始化表格是否成功,若成功则返回0, 否则返回-1
2 输出交换单元格是否成功
3 输出插入行是否成功
4 输出插入列是否成功
5 输出查询单元格数据是否成功

示例1

输入

4 9
5 1 2 6
0
8
2 3
4 7
4 2 3 2
3
3
4 7

输出

0
-1
0
-1
0
0
-1
0
0
-1

说明

本组样例共有2组样例输入。
第一组样例:
1.初始化数据表为4行9列,成功
2.交换第5行1列和第2行6列的数据,失败。因为行的范围应该是(0,3),不存在第5行。
3.在第0行上方添加一行,成功。
4.在第8列左边添加一列,失败。因为列的总数已经达到了9的上限。
5.查询第2行第3列的值,成功。
第二组样例:
1.初始化数据表为4行7列,成功
2.交换第4行2列和第3行2列的数据,失败。因为行的范围应该是(0,3),不存在第4行。
3.在第3行上方添加一行,成功。
4.在第3列左边添加一列,成功。
5.查询第4行7列的值,失败。因为虽然添加了一行一列,但数据表会在添加后恢复成4行7列的形态,所以行的区间仍然在[0,3],列的区间仍然在[0,6],无法查询到(4,7)坐标。      
while True:
    try:
        s1 = [int(i) for i in input().split()]
        s2 = [int(i) for i in input().split()]
        s3 = int(input())
        s4 = int(input())
        s5 = [int(i) for i in input().split()]
        if s1[0] <= 9 and s1[1] <= 9:
            print(0)
        else:
            print(-1)
        if s2[0] < s1[0] and s2[2] < s1[0] and s2[1] < s1[1] and s2[3] < s1[1] :
            print(0)
        else:
            print(-1)
        if s3 < s1[0] < 9:
            print(0)
        else:
            print(-1)
        if s4 < s1[1] < 9:
            print(0)
        else:
            print(-1)
        if s5[0] < s1[0] and s5[1] < s1[1] :
            print(0)
        else:
            print(-1)
    except:
        break

发表于 2024-12-24 18:50:57 回复(0)
用本地VS写的,提交后才发现,不能用numpy库....

# input and inital matrix(m,n)
# martrix = [[0 for _ in range(4)] for _ in range(3)]
import numpy as np

try:
    input_m_n = list(map(int, input("").split()))
    m = input_m_n[0]
    n = input_m_n[1]
    if m > 9 or n > 9:
        raise ValueError
    matrix = np.zeros((m, n))
    print(0)

except ValueError:
    print(-1)

# 2. exchange (x1,y1),(x2,y2)
try:
    input_exchange = list(map(int, input("").split()))
    x1 = input_exchange[0]
    y1 = input_exchange[1]
    x2 = input_exchange[0]
    y2 = input_exchange[1]

    if x1 > m or x2 > m or y1 > n or y2 > n:
        raise ValueError

    exchange = []  # for value exchange
    matrix[x1][y1] = exchange
    matrix[x1][y1] = matrix[x2][y2]
    matrix[x2][y2] = exchange
    print(0)
except ValueError:
    print(-1)

# 3.insert row, by using 'np.insert(arr, obj, values, axis=0)'
# (obj = 1 --> insert one row above 1.st row [1])
try:
    input_row_insert = int(input(""))
    len_rows = len(matrix[:, 0])  # 切片 array[m,n], 列的元素数 = 行数m
    if input_row_insert > m - 1 or len_rows == 9:  # 已经到最大值,不管在哪里插入一行,都超出
        raise ValueError
    else:
        new_matrix = np.insert(matrix, input_row_insert, [1] * n, axis=0)  # 行的元素数是列数
    print(0)
except ValueError:
    print(-1)

# 4.insert column, by using 'np.insert(arr, obj, values, axis=1)'
# (obj = 1 --> insert one column left aside of 1.st column)
try:
    input_col_insert = int(input(""))
    len_col = len(matrix[0, :])  # 切片 array[m,n], 行的元素数 = 列数n
    if input_col_insert > n or len_col == 9:
        raise ValueError
    else:
        new_matrix = np.insert(matrix, input_col_insert, [1] * m, axis=1)  # 列的元素数是行数
    print(0)
except ValueError:
    print(-1)

# 5. find value of (x,y)
try:
    input_val_x_y = list(map(int, input("").split()))
    x = input_val_x_y[0]
    y = input_val_x_y[1]
    if x > m - 1 or y > n - 1:
        raise ValueError
    val_x_y = matrix[x][y]
    print(0)
except ValueError:
    print(-1)
发表于 2024-08-19 12:54:49 回复(0)
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())

        arr, tmp = [], []
        if m > 9&nbs***bsp;n > 9:
            print(-1)
            print(-1)
            print(-1)
            print(-1)
            print(-1)
        else:
            arr = [[0 for j in range(m)] for i in range(n)]  # 初始化 arr
            tmp = arr.copy()  # 备份 arr
            print(0)
            if not (0 <= x1 <= m-1 and 0 <= x2 <= m-1 and 0 <= y1 <= n-1 and 0 <= y2 <= n-1):
                print(-1)
            else:
                arr[y1][x1], arr[y2][x2] = arr[y2][x2], arr[y1][x1]  # 交换值
                tmp = arr.copy()  # 更新 tmp
                print(0)
            if m+1 >= 10&nbs***bsp;m+1<0&nbs***bsp;m-1<r&nbs***bsp;r<0:
                print(-1)
            else:
                for i in range(0,n):
                    arr[i].append(0)
                    for j in range(m, -1, -1):
                        if j > r:
                            arr[i][j] = arr[i][j-1]
                        elif j == r:
                            arr[i][j] = 0
                arr = tmp.copy()  # 还原
                print(0)
            if n+1 >= 10&nbs***bsp;n+1<0&nbs***bsp;n-1<c&nbs***bsp;c<0:
                print(-1)
            else:
                arr.append([])
                for i in range(n, -1, -1):
                    for j in range(0,m):
                        arr[n].append(0)
                        if i > c:
                            arr[i][j] = arr[i-1][j]
                        elif i == c:
                            arr[i][j] = 0
                arr = tmp.copy()  # 还原
                print(0)
            if not (0 <= x <= m-1 and 0 <= y <= n-1):
                print(-1)
            else:
                print(0)
    except:
        break

发表于 2024-05-31 14:34:16 回复(1)
import sys

while True:
    try:
        m,n = map(int, input().split())
        if m>9&nbs***bsp;n>9:
            print('-1') 
            
        else:
            print('0')

        x1, y1, x2, y2 = map(int, input().split())
        if x1 <= m-1 and x2 <= m-1 and y1 <= n-1 and y2 <= n-1:
            print('0')

        else:
            print('-1')

        insert_row = int(input())
        if insert_row < m and m + 1<= 9:
            print('0')

        else:
            print('-1')


        insert_col = int(input())
        if insert_col < n and n + 1<= 9:
            print('0')

        else:
            print('-1')


        x, y = map(int, input().split())
        if x <= m-1 and y <= n-1:
            print('0')
        else:
            print('-1')

    except:
        break

发表于 2023-11-01 20:31:45 回复(0)
while True:
    try:
        row, col = map(int, input().strip().split())
        x1, y1, x2, y2 = map(int, input().strip().split())
        insert_row = int(input())
        insert_col = int(input())
        x, y = map(int, input().strip().split())

        flag = False
        if row < 0&nbs***bsp;col < 0:
            print(-1)
            flag = True
        else:
            print(0)
        
        if flag:
            for i in range(4):
                print(-1)
        else:
            if (
                (x1 >= 0 and x1 <= row - 1)
                and (x2 >= 0 and x2 <= row - 1)
                and (y1 >= 0 and y1 <= col - 1)
                and (y1 >= 0 and y1 <= col - 1)
            ):
                print(0)
            else:
                print(-1)

            if row <= 8 and insert_row >= 0 and insert_row <= row-1:
                print(0)
            else:
                print(-1)

            if col <= 8 and insert_col >= 0 and insert_col <= col-1:
                print(0)
            else:
                print(-1)

            if (
                (x >= 0 and x <= row - 1)
                and (y >= 0 and y <= col - 1)
            ):
                print(0)
            else:
                print(-1)
        # print("-------------")

    except:
        break

发表于 2023-09-28 22:05:09 回复(0)
while True:

    try:
        initial  = input().split()
        m, n = int(initial[0]), int(initial[1])
        if m > 9&nbs***bsp;n > 9:
            print('-1')
        else:
            print('0')
        
        exchange = input().split()
        x1, y1, x2, y2 = int(exchange[0]), int(exchange[1]), int(exchange[0]), int(exchange[1])
        if x1 <= m-1 and x2 <= m-1 and y1 <= n-1 and y2 <= n-1:
            print('0')
        else:
            print('-1')

        addrow = int(input())
        if addrow > m-1&nbs***bsp;m + 1 > 9 :
            print('-1')
        else:
            print('0')

        addcolumn = int(input())
        if addcolumn > n-1&nbs***bsp;n + 1 > 9:
            print('-1')
        else:
            print('0')

        site = input().split()
        x, y = int(site[0]), int(site[1])
        if x <= m-1 and y <= n-1:
            print('0')
        else:
            print('-1')
    except:
        break

发表于 2023-09-25 13:06:06 回复(0)

循环

while True:
    try:
        l = list(map(int, input().split()))
        m, n = l[0], l[1]
        if m >= 1 and n >= 1:
            print(0)
        else:
            print(-1)
        l = list(map(int, input().split()))
        x1, y1, x2, y2 = l[0], l[1], l[2], l[3]
        if x1 >= 0 and x1 < m and y1 >= 0 and y1 < n and x2 >= 0 and x2 < m and y2 >= 0 and y2 < n:
            print(0)
        else:
            print(-1)
        x = int(input())
        if x >= 0 and x < m and m < 9:
            print(0)
        else:
            print(-1)
        y = int(input())
        if y >= 0 and y < n and n < 9:
            print(0)
        else:
            print(-1)
        l = list(map(int, input().split()))
        x, y = l[0], l[1]
        if x >= 0 and x < m and y >= 0 and y < n:
            print(0)
        else:
            print(-1)
    except:
        break
发表于 2023-06-30 15:47:00 回复(0)
import sys

def test_xy(cor,bound):
    if 0<=cor[0]<=bound[0] and 0<=cor[1]<=bound[1]:
        return True
    else:
        return False

count=0
ope=[]
for line in sys.stdin:
    if count<4:
        ope.append([int(i) for i in line.split(' ')])
        count+=1
    else:
        ope.append([int(i) for i in line.split(' ')])
        m,n=ope[0][0]-1,ope[0][1]-1
        print(-1) if m>8&nbs***bsp;n>8 else print(0)
        print(0) if test_xy(ope[1][:2],[m,n]) and test_xy(ope[1][2:],[m,n]) else print(-1)
        print(-1) if m+1>8&nbs***bsp;ope[2][0]>m else print(0)
        print(-1) if n+1>8&nbs***bsp;ope[3][0]>n else print(0)
        print(0) if test_xy(ope[4],[m,n]) else print(-1)
        count=0
        ope=[]

发表于 2023-05-04 15:13:58 回复(0)
# J83二维数组操作
while True:
    try:
        # 输入
        m, n = [int(i) for i in input().rsplit()]
        x_1, y_1, x_2, y_2 = [int(i) for i in input().rsplit()]
        x_3 = int(input())
        y_4 = int(input())
        x_5, y_5 = [int(i) for i in input().rsplit()]
        
        # 初始化
        if m <= 9 and n <= 9:
            print("0")
        else:
            print("-1")
        # 交换单元格
        if x_1 < m and x_2 < m and y_1 < n and y_2 < n:
            print("0")
        else:
            print("-1")
        # 插入行
        if 8 >= m >= x_3+1:
            print("0")
        else:
            print("-1")
        # 插入列
        if 8 >= n >= y_4+1:
            print("0")
        else:
            print("-1")
        # 查询
        if x_5 < m and y_5 < n:
            print("0")
        else:
            print("-1")
    except:
        break
发表于 2023-03-25 16:24:17 回复(0)
while True:
    try:
        m,n=map(int,input().split())
        x1,y1,x2,y2=map(int,input().split())
        xch=int(input())
        ycl=int(input())
        xc,yc=map(int,input().split())
       
        if m<=9 and n<=9:
            print(0)
        else:
            print(-1)
        if x1<m and y1<n and x2<m and y2<n:
            print(0)
        else:
            print(-1)
        if m<8:
            print(0)
        else:
            print(-1)
        if n<8: #为什么n是7的时候不能char一列  没有超出呀
            print(0)
        else:
            print(-1)
        if xc<m and yc<n:
            print(0)
        else:
            print(-1)
       
    except:
        break
发表于 2023-03-12 22:44:38 回复(0)
while True:
    try:
        #接收输入
        m, n = map(int, input().split())
        x1, y1, x2, y2 = map(int, input().split())
        x = int(input())
        y = int(input())
        x3, y3 = map(int, input().split())

        #判断输出
        print(0 if -1 < m < 10 and -1 < n < 10 else -1)
        print(0 if -1 < x1 < m and -1 < y1 < n and -1 < x2 < m and -1 < y2 < n else -1)
        print(0 if -1 < x < m < 9 else -1)
        print(0 if -1 < y < n < 9 else -1)
        print(0 if -1 < x3 < m and -1 < y3 < n else -1)

    except:
        break

发表于 2023-02-28 00:14:15 回复(0)
while True:
try:
m, n = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x, y = int(input()), int(input())
x3, y3 = map(int, input().split())
print(0 if m <= 9 and n <= 9 else -1)
print(0 if x1 < m and y1 < n and x2 < m and y2 < n else -1)
print(0 if x < m < 9 else -1)
print(0 if y < n < 9 else -1)
print(0 if x3 < m and y3 < n else -1)
except:
break
发表于 2023-02-22 14:53:44 回复(0)
while True:
    try:
        m,n=map(int,input().split(' '))#第1次输入
        if 1<m<10 and 1<n<10:
            print(0)
            x1,y1,x2,y2=map(int,input().split(' '))#第2次输入
            if 0<=x1<m and 0<=x2<m and 0<=y1<n and 0<=y2<n:
                print(0)
            else:
                print(-1)
            x=int(input())#第3次输入
            if 0<=x<m and m<9:
                print(0)
            else:
                print(-1)
            y=int(input())#第4次输入
            if 0<=y<n and n<9:
                print(0)
            else:
                print(-1)
            x3,y3=map(int,input().split(' '))#第5次输入
            if 0<=x3<m and 0<=y3<n:
                print(0)
            else:
                print(-1)
        else:
            a2=input()
            a3=input()
            a4=input()
            a5=input()
            print(-1)
            print(-1)
            print(-1)
            print(-1)
            print(-1)
    except:
        break
表格初始化失败后就不用做后面的判断了,直接全部-1
发表于 2023-02-14 18:20:38 回复(0)
while True:
    try:
        m, n = map(int, input().split())
        x1, y1, x2, y2 = map(int, input().split())
        insert_row = int(input())
        insert_col = int(input())
        show_x, show_y = map(int, input().split())
    except EOFError:
        break
    # 初始化
    s = 0
    print(s)
    # 交换
    s = 0 if 0 <= x1 <= m - 1 and 0 <= y1 <= n - 1 else -1
    s = s if 0 <= x2 <= m - 1 and 0 <= y2 <= n - 1 else -1
    print(s)
    # 行插入
    s = 0 if insert_row < m <= 8 else -1
    print(s)
    # 列插入
    s = 0 if insert_col < n <= 8 else -1
    print(s)
    # 查询
    s = 0 if 0 <= show_x <= m - 1 and 0 <= show_y <= n - 1 else -1
    print(s)

发表于 2023-02-04 11:45:24 回复(0)
# 看用例改代码
while True:
    try:
        m_n = input()  # 表格的行列值
        exchange_m_n = input()  # 要交换的两个单元格的行列值
        insert_m = int(input())  # 要插入的行的数值
        insert_n = int(input())  # 要插入的列的数值
        m1_n1 = input()  # 要查询的单元格的坐标
        list_m_n = list(m_n.split())  # 按空格字符串劈分m_n,转化成列表赋值给list_m_n
        list_exchange_m_n = list(exchange_m_n.split())  # 按空格字符串劈分exchange_m_n,转化成列表赋值给list_exchange_m_n
        list_m1_n1 = list(m1_n1.split())  # 按空格字符串劈分m1_n1,转化成列表赋值给list_m1_n1
        if int(list_m_n[0]) > 9 or int(list_m_n[1]) > 9:  # 行大于9或者列大于9
            print(-1)
        else:
            print(0)
        # 检查要交换的两个单元格的行列值,行号坐标只允许0~m-1,列号坐标只允许0~n-1
        if int(list_exchange_m_n[0]) >= int(list_m_n[0]) or int(list_exchange_m_n[1]) >= int(list_m_n[1]) or int(
                list_exchange_m_n[2]) >= int(list_m_n[0]) or int(list_exchange_m_n[3]) >= int(list_m_n[1]):
            print(-1)
        else:
            print(0)
        if insert_m >= int(list_m_n[0]) or int(list_m_n[0]) + 1 > 9:  # 插入行后是否大于9或者超出行范围插入
            print(-1)
        else:
            print(0)
        if insert_n >= int(list_m_n[1]) or int(list_m_n[1]) + 1 > 9:  # 插入列后是否大于9或者超出列范围插入
            print(-1)
        else:
            print(0)
        if int(list_m1_n1[0]) >= int(list_m_n[0]) or int(list_m1_n1[1]) >= int(
                list_m_n[1]):  # 行号坐标只允许0~m-1,列号坐标只允许0~n-1
            print(-1)
        else:
            print(0)
    except:
        break
发表于 2022-05-21 22:42:37 回复(0)
insertrow = int(input())
        # 判断插入行后是否超出范围
        # 插入1行不是应该y坐标可能越界吗,为什么都在判断x????
        ifinsertrow >= x or insertrow < 0or x + 1> 9:
            print(-1)
        else:
            print(0)
        insertcol = int(input())
        # 判断插入列后是否超出范围
        ifinsertcol >= y or insertcol < 0or y + 1> 9:        # 插入列也是为什么会去判断y坐标???
            print(-1)
        else:
            print(0)

发表于 2022-04-25 23:02:07 回复(0)
#  程序在本地idle上可以通过,但是在线测试超时不能通过

while True:
    try:
        try: # 表格行列值
            m, n = map(int, input().strip().split())
            l = []
            for i in range(m):
                l.append([])
                for j in range(n):
                    l[i].append(0)
            print(0)
        except:
            print(-1)

        try:  #交换两个单元格的值
            x1, y1, x2, y2 = map(int, input().strip().split())
            l[x1][y1] = l[x2][y2]
            print(0)
        except:
            print(-1)

        try:     #插入行
            m1 = int(input().strip())
            lm = []
            for i in range(n):
                lm.append(0)
            l.insert(m1, lm)
            l.pop(len(l) - 1)
            print(0)
        except:
            print(-1)

        try:        # 插入列,先转置行列,将插入列变为插入行,最后再转置行列
            n1 = int(input().strip())
            ln = []
            for i in range(m):
                ln.append(0)

            lt = []
            for i in range(n):
                lt.append([])
                for j in range(m):
                    lt[i].append(0)
            for i in range(n):
                for j in range(m):
                    lt[i][j] = lt[j][i]

            lt.insert(n, ln)
            lt.pop(len(lt) - 1)

            l = []
            for i in range(n):
                l.append([])
                for j in range(m):
                l[i].append(0)
            for i in range(n):
                for j in range(m):
                    l[i][j] = lt[j][i]

            for i in range(m):
                l.append([])
                for j in range(n):
                    l[i][j] = lt[j][i]
            print(0)
        except:
            print(-1)

        try:  # 查询某个单元格的值
            x1, y1 = map(int, input().strip().split())
            print(l[x1][y1])
        except:
            print(-1)

    except:
        break

发表于 2022-01-05 15:11:19 回复(0)