牛客模拟笔试
第一题
import java.util.*; public class Solution { public static void main(String[] args) { var res = new Solution().findMaxButtons(new int[]{2,2,2}); System.out.println(res); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param buttons int整型一维数组 * @return long长整型 */ public long findMaxButtons (int[] buttons) { if (buttons == null) { return 0; } long res = 0; for (int i = 0; i < buttons.length; ++i) { long ai = buttons[i] + (long)(buttons[i] - 1) * i; res += ai; } return res; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt(); String line = sc.next(); char[] chs = line.toCharArray(); int[] a = new int[len]; int[] b = new int[len]; if (chs[0] == '0') { a[0] = 1; } for (int i = 1; i < len; ++i) { a[i] = chs[i] == '0' ? a[i-1] + 1 : a[i-1]; } if (chs[len-1] == '1') { b[len-1] = 1; } for (int i = len - 2; i >= 0; --i) { b[i] = chs[i] == '1' ? b[i+1] + 1 : b[i+1]; } int res = Math.min(a[len-1], b[0]); for (int i = 1; i < len; ++i) { if (a[i-1] + b[i] < res) { res = a[i-1] + b[i]; } } System.out.println(res); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s1 = sc.next(); String s2 = sc.next(); String s3 = sc.next(); Player[] plays = new Player[3]; plays[0] = new Player("xiaoming", count(s1, n)); plays[1] = new Player("xiaowang", count(s2, n)); plays[2] = new Player("xiaoli", count(s3, n)); Arrays.sort(plays, (o1, o2)->{ return o1.value - o2.value; }); if (plays[1].value == plays[2].value) { System.out.println("draw"); } else { System.out.println(plays[2].name); } } static class Player { String name; int value; Player(String name, int value) { this.name = name; this.value = value; } } private static int count(String s, int n) { int[] cnt = new int[128]; char[] chs = s.toCharArray(); for (int i = 0; i < chs.length; ++i) { cnt[chs[i]]++; } int maxV = 0; for (int i = 0; i < 128; ++i) { if (cnt[i] > maxV) { maxV = cnt[i]; } } return maxV + n >= s.length() ? s.length() : maxV + n; } }
全部评论
(4) 回帖