第二题小朋友猜拳,测试用例过,考虑到了起点可变等,但就是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) 回帖