2023.03.23—腾讯音乐笔试
小红拿到了一个仅由大写字母和小写字母组成的字符串。她想知道,在不考虑大小写的情况下,有多少对相邻的字母相等?字符串长度不超过2x10^5。
示例输入
aABbbc
输出
3
public class Test { public static int test_1(String str) { int len = str.length(); int count = 0; for (int i = 0; i < len - 1; i++) { char char1 = Character.toLowerCase(str.charAt(i)); char char2 = Character.toLowerCase(str.charAt(i+1)); if (char1 == char2) { count += 1; } } return count; } public static void main(String[] args) { String str1 = "aABbbc"; System.out.println(test_1(str1)); } }
|
阿里笔试
一个球队有n个球员,已知他们一共接受了x张黄牌,y张红牌。当一个球员满足以下条件之一时会立刻下场: 1.接受了2张黄牌 2.接受了1张红牌。 请问己知以上信息的前提下,当前能上场的球员最多少人?最少有多少人?共有q次查询。
第一行输入一个正整数q,代表查询的次数。
接下来的q行,每一行输入三个整数n,x,y,代表一次查询。
$1 \leq q \leq 10^4 , 1 \leq n \leq 10^9, 0 < x , y < 10^9$
保证至少存在一种合法情况(即不会所有人都被罚下场了继续接受到黄牌/二牌)
输出q行,每一行输出两个整数max,min,代表当前能上场球员数量的最大值和最小值
import java.util.*;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); int[][] result = new int[q][2]; for (int i = 0; i < q; i++) { int n = scanner.nextInt(); int x = scanner.nextInt(); int y = scanner.nextInt(); int max = calculateMax(n, x, y); int min = calculateMin(n, x, y); result[i][0] = max; result[i][1] = min; } for (int i = 0; i < q; i++) { System.out.println(result[i][0] + " " + result[i][1]); } } private static int calculateMax(int n, int x, int y) { int less = n - y; int yellow = less - x; if (yellow > 0) return less; else return (2*less - x); } private static int calculateMin(int n, int x, int y) { int less = n - y; less = (int) Math.ceil((2*less-x)/2.0); return less; } }
|
我们定义一个数组的权值为:所有元素乘积的因子数量。例如,$[2,6]$的权值为6,因为$2*6=12$,12有6个因子:$ (1,2,3,4,6,12)$,现在给定一个数组,试求该数组有多少连续子数组的权值不小于k?
输入:
第一行输入两个正整数n和k。
第二行输入n个正整数a_i;,代表给定的数组。
1≤n,a_i≤200000
1≤k≤10^9
输出:
权值不小于k的连续子数组数量。
输入:
4 4
2 3 1 3
输出:
3
import java.util.*;
public class Main { public static int getYinZi(int num) { if (num == 1) return 1; int flag = 0; for (int i = 1; i < Math.sqrt(num); i++) { if (num % i == 0) { flag += 2; } } return flag; } public static int getWeight(int[] nums, int w) { int result = 0; for (int i = 0; i < nums.length; i++) { for (int j = i; j < nums.length; j++) { int sum = 1; for (int k = i; k <= j; k++) { sum *= nums[k]; } int weight = getYinZi(sum); if (weight >= w) result++; } } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = scanner.nextInt(); } System.out.println(getWeight(nums, k)); } }
|