解题思路

这是一个二维数组操作问题,需要实现以下5种操作:

  1. 初始化 大小的表格
  2. 交换两个坐标的数据
  3. 插入行
  4. 插入列
  5. 查询指定坐标的值

关键点:

  1. 需要验证所有输入的合法性
  2. 表格大小限制在
  3. 坐标从0开始计数
  4. 本题包含多组输入数据

代码

#include <iostream>
using namespace std;

int main() {
    int m, n, x1, y1, x2, y2, x, y, x3, y3;
    while (cin >> m >> n) {
        cin >> x1 >> y1 >> x2 >> y2;
        cin >> x >> y;
        cin >> x3 >> y3;
        
        // 初始化判断
        cout << (m <= 9 && n <= 9 ? 0 : -1) << endl;
        
        // 交换坐标判断
        cout << (x1 < m && x2 < m && y1 < n && y2 < n ? 0 : -1) << endl;
        
        // 插入行判断
        cout << (9 > m && m > x ? 0 : -1) << endl;
        
        // 插入列判断
        cout << (9 > n && n > y ? 0 : -1) << endl;
        
        // 查询坐标判断
        cout << (x3 < m && y3 < n ? 0 : -1) << endl;
    }
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            int x3 = sc.nextInt();
            int y3 = sc.nextInt();
            
            // 初始化判断
            System.out.println(m <= 9 && n <= 9 ? 0 : -1);
            
            // 交换坐标判断
            System.out.println(x1 < m && x2 < m && y1 < n && y2 < n ? 0 : -1);
            
            // 插入行判断
            System.out.println(9 > m && m > x ? 0 : -1);
            
            // 插入列判断
            System.out.println(9 > n && n > y ? 0 : -1);
            
            // 查询坐标判断
            System.out.println(x3 < m && y3 < n ? 0 : -1);
        }
    }
}
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 x2 < m and y1 < n and y2 < n else -1)
        
        # 插入行判断
        print(0 if 9 > m > x else -1)
        
        # 插入列判断
        print(0 if 9 > n > y else -1)
        
        # 查询坐标判断
        print(0 if x3 < m and y3 < n else -1)
    except:
        break

算法及复杂度

  • 算法:简单的条件判断
  • 时间复杂度: - 每组输入只需要常数时间的判断
  • 空间复杂度: - 只需要存储几个整数变量