牛客刷题记录1

题1:坐标求和

java 复制代码
import java.util.*;

class Solution {
    public long sumSelected(long[][] matrix, List<int[]> coords) {
        long total = 0;
        for (int[] opt : coords) {
            total += matrix[opt[0]][opt[1]];
        }
        return total;
    }
}


public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        if(!sc.hasNextInt())
            break;
        int m = sc.nextInt();
        if(!sc.hasNextInt())
            break;
        int n = sc.nextInt();

        long[][] matrix = new long[m][n];

        //注意这里不能用while  因为后面还有输入  直接进入死循环
        // while(sc.hasNextInt()) {
        //     for (int i = 0; i < m; i++) {
        //         for (int j = 0; j < n; j++) {
        //             matrix[i][j] = sc.nextLong();
        //         }
        //     }
        // }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextLong();
            }
        }

        if(!sc.hasNextInt())
            break;
        int q = sc.nextInt();

        List<int[]> list = new ArrayList<>(q);
        while(sc.hasNextInt()) {
            for (int i = 0; i < q; i++) {
                int r = sc.nextInt();
                int c = sc.nextInt();

                list.add(new int[] {r, c});
            }
        }

        System.out.println(solution.sumSelected(matrix, list));
        
    }
    sc.close();
}

题2:坐标移动

正则表达式好用!

  • [abc] - 匹配 a、b 或 c 中的一个字母 测验
  • [a-z] - 匹配 a 到 z 中的一个字母 测验
  • [^abc] - 匹配除了 a、b 或 c 中的其他字母测验
  • {n} - 匹配 n测验
  • {n,} - 匹配 n 次以上 测验
  • {m ,n} - 最少 m 次,最多 n 次匹配 测验
java 复制代码
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //输入处理,并对字符进行分割
        Scanner sc = new Scanner(System.in);
        String[] in = sc.nextLine().split(";");


        //设置坐标原始值
        int x = 0;
        int y = 0;

        //对分割之后的每个字符进行处理
        for (String s : in) {
            //对字符进行匹配,因为最多两位数,所以就最少匹配1次,最多匹配2次
            if (!s.matches("[WASD][0-9]{1,2}")) {
                continue;
            }
            //截取数字部分并转换为整数
            int step = Integer.valueOf(s.substring(1));
            //对第一位字符进行方向匹配,并做对应的移动
            switch (s.charAt(0)) {
                case 'W':
                    y += step;
                    break;
                case 'S':
                    y -= step;
                    break;
                case 'A':
                    x -= step;
                    break;
                case 'D':
                    x += step;
                    break;
            }
        }
        //输出结果
        System.out.println(x + "," + y);
        //关闭scanner
        sc.close();

        
    }
}
相关推荐
丢掉幻想准备斗争3 分钟前
5.5树与二叉树的应用
算法
ZhouDevin29 分钟前
算法论文/模型微调2——仅骨干1%~3% 参数达到与全量微调相当的性能
算法
Sinosecu-OCR42 分钟前
文通OCR技术深度解析:自研算法如何搞定复杂场景识别?
算法·ocr·ocr识别系统
CQU_JIAKE1 小时前
8.5【A】
数据结构·算法
ESBK20251 小时前
学术邀约|CMICA 2026 第二届计算方法、智能控制与航空航天国际会议
大数据·人工智能·算法·飞行器·智能控制·航空航天·计算
无bug代码搬运工1 小时前
LeetCode 42 接雨水:双指针解法详解
算法·leetcode·职场和发展
Jasmine_llq2 小时前
《P13016 [GESP202506 六级] 最大因数》
数据结构·算法
白狐_7982 小时前
考研408算法设计题保命策略:链表专题暴力解法精讲(2015 & 2019真题实战)
考研·算法·链表
fangpin2 小时前
GPU编程2: 并行策略
算法
碧海银沙音频科技研究院2 小时前
8基于BES2700IHC数字助听器系统开发
嵌入式硬件·算法