题解 | #二维数组操作#

二维数组操作

https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            //1. m <=9, n <= 9
            int m = in.nextInt();
            int n = in.nextInt();
            if (m > 9 || n > 9) {
                System.out.println(-1);
            } else {
                System.out.println(0);
            }

            //2. 这个交换前提是在[0~m-1][0~n-1]范围内
            int x1 = in.nextInt();
            int y1 = in.nextInt();
            int x2 = in.nextInt();
            int y2 = in.nextInt();
            if (x1 >= m || y1 >= n || x2 >= m || y2 >= n) {
                System.out.println(-1);
            } else {
                System.out.println(0);
            }

            //注意!!!: 无论插入行还是列,
            //1) 插入后,要求整体行数/列数 <= 9
            //2) 插入时,插入的位置要在[0~m-1][0~n-1]内,否则也失败

            //3 插入列
            int x = in.nextInt(); // 上方添加一列
            if (m >= 9 || x >= m) {
                System.out.println(-1);
            } else {
                System.out.println(0);
            }

            //4 插入列
            int y = in.nextInt(); //左列添加一列
            if (n >= 9 || y >= n) {
                System.out.println(-1);
            } else {
                System.out.println(0);
            }


            //5.查找(x,y) 单元格的值
            int xx = in.nextInt();
            int yy = in.nextInt();
            if (xx >= m || yy >= n) {
                System.out.println(-1);
            } else {
                System.out.println(0);
            }
        }
    }
}

全部评论

相关推荐

昨天 19:03
门头沟学院 运营
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
董春花_:真诚无罪,别听评论区那个清华的。按他的逻辑,你有分寸人觉得你是不想来,你积极热情人觉得你太想来,你好骗人就可你养鱼,你不好骗人觉得你服从性不高,合着**做啥都白扯。保持谦逊礼貌与对offer的积极性不才是最正常,也正确的做法么?招聘方的错强加到应聘者身上,***何不食肉糜。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务