首页 > 网易互娱4.18笔试
头像
致命范进
编辑于 2021-04-19 17:24
+ 关注

网易互娱4.18笔试

第二题小朋友猜拳,测试用例过,考虑到了起点可变等,但就是0%😭
求AC代码学习学习!附上我的代码(Java实现),求大佬指点!
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = Integer.parseInt(in.nextLine());
        int[] ans = new int[t];
        for (int i = 0; i < t; i++) {
            String[] s = in.nextLine().trim().split("\\s");
            int n = Integer.parseInt(s[0]);
            int m = Integer.parseInt(s[1]);
            char[] way = in.nextLine().trim().toCharArray();
            ans[i] = findMaxRemain(n, m, way);
        }
        for (int i = 0; i < t; i++) {
            System.out.println(ans[i]);
        }
    }

    public static int findMaxRemain(int n, int m, char[] way) {
        int maxRemain = 1;
        //每个起点都试
        for (int i = 0; i < n; i++) {
            LinkedList<Character> child = new LinkedList<>();
            for (int j = 0; j < way.length; j++) {
                child.add(way[j]);
            }
            int index = i;
            for (int j = 0; j < m; j++) {
                index = index % child.size();
                int next = (index + 1) % child.size();
                int result = pk(child.get(index), child.get(next));
                if (result > 0) {
                    child.remove(next);
                } else if (result < 0) {
                    child.remove(index);
                } else {
                    index++;
                }
                if (child.size() == 1) {
                    break;
                }
            }
            maxRemain = Math.max(maxRemain, child.size());
        }
        return maxRemain;
    }
    //猜拳
    public static int pk(char a, char b) {
        if (a == b)
            return 0;
        else if ((a == 'R' && b == 'S') || (a == 'S' && b == 'C') || (a == 'C' && b == 'R'))
            return 1;
        else
            return -1;
    }
}    


全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐