题解 | #二维数组操作#
二维数组操作
https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
while (getline(cin, s)) {
int m = s[0] - '0', n = s[2] - '0';
if ((m <= 0 || m > 9) || (n <= 0 || n > 9)) {
cout << "-1" << endl;
} else {
cout << "0" << endl;
}
// vector<vector<int>> arr(m,vector<int>(n));
getline(cin, s);
int x1 = s[0] - '0', y1 = s[2] - '0', x2 = s[4] - '0', y2 = s[6] - '0';
if ((x1 < 0 || x1 >= m) || (x2 < 0 || x2 >= m) || (y1 < 0 || y1 >= n) ||
(y2 < 0 || y2 >= n)) {
cout << "-1" << endl;
} else {
cout << "0" << endl;
}
// int tmp = arr[x1][y1];
// arr[x1][y1] = arr[x2][y2];
// arr[x2][y2] = tmp;
getline(cin, s);
int x = s[0] - '0';
if ((x < 0 || x >= m) || (m + 1 > 9)) {
cout << "-1" << endl;
} else {
cout << "0" << endl;
}
getline(cin, s);
int y = s[0] - '0';
if ((y < 0 || y >= n) || (n + 1 > 9)) {
cout << "-1" << endl;
} else {
cout << "0" << endl;
}
getline(cin,s);
x1 = s[0] - '0';
y1 = s[2] - '0';
if((x1 < 0 || x1 >= m) || (y1 < 0 || y1 >= n)){
cout << "-1" << endl;
}else{
cout << "0" << endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
注意对题目意思的理解,题目应该只是要求看这个操作是否合法,并不需要实际去执行这个操作。

查看26道真题和解析