【问题描述】输入两个同为mn的二维整数数组A和B(m,n<10),计算A-B的差并输出。
【输入形式】m|n|第1个mn二维数组A|第2个mn二维数组B
【输出形式】mn二维数组C
【样例输入】
2
3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
【样例输出】
2 4 6
8 10 12
【问题描述】输入两个同为mn的二维整数数组A和B(m,n<10),计算A-B的差并输出。
【输入形式】m|n|第1个mn二维数组A|第2个mn二维数组B
【输出形式】mn二维数组C
【样例输入】
2
3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
【样例输出】
2 4 6
8 10 12
【以下回答由 GPT 生成】
def subtract_array(A, B):
result = [[A[i][j] - B[i][j] for j in range(len(A[0]))] for i in range(len(A))]
return result
m = int(input("Enter the number of rows: "))
n = int(input("Enter the number of columns: "))
# Taking input for array A
A = []
print("Enter the elements for array A:")
for i in range(m):
row = list(map(int, input().split()))
A.append(row)
# Taking input for array B
B = []
print("Enter the elements for array B:")
for i in range(m):
row = list(map(int, input().split()))
B.append(row)
# Calculating the difference
C = subtract_array(A, B)
# Printing the result array C
print("The result array C is:")
for row in C:
print(' '.join(map(str, row)))
This code takes the input for two arrays A and B, calculates the difference, and prints the result array C. It is a simple and effective solution for finding the difference between two 2D arrays.
【相关推荐】