阿里巴巴2021秋招笔试题20211119
程序员文章站
2022-06-09 09:23:50
...
源代码:https://gitee.com/shentuzhigang/algorithm/tree/master/exam-alibaba/exam-alibaba-20211119
第一题
题目大意:
有长度为n的数组a
有k次机会在连续长度不超过m的区间每个元素+1使得数组全部元素变成偶数
import java.util.LinkedList;
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email [email protected]
* @date 2021-11-19 19:16
*/
public class Exam2021111901 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int m = scanner.nextInt();
int[] a = new int[n];
int add = 0;
LinkedList<Integer> q= new LinkedList<>();
for (int j = 0; j < n; j++) {
a[j] = scanner.nextInt();
if ((a[j]+add)%2==1){
if(q.isEmpty()){
k--;
add++;
q.push(j+m-1);
}else{
add--;
q.pop();
}
}
if(q.peek()!=null&&q.peek()==j){
q.pop();
add--;
}
}
System.out.println(k>=0?"Yes":"No");
}
}
}
第二题
(未验证正确性)
题目大意:
有 单词组 strs
人数n,长度b
每个人按单词组依次报【0,i】
例:
king
king hello
king hello wrold
king hello
king
king hello
king hello wrold
king hello
求单词word第X次出现在第几行
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email [email protected]
* @date 2021-11-19 20:36
*/
public class Exam2021111902 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int b = scanner.nextInt();
scanner.nextLine();
String[] s = scanner.nextLine().split(" ");
String word = s[0];
int c = Integer.parseInt(s[1]);
String[] strs = scanner.nextLine().split(" ");
if(b==1){
System.out.println(b);
System.exit(0);
}
int t = 0;
for (int i = 0; i < 2*b-2; i++) {
for (int j = 0; j <= i; j++) {
if(word.equals(strs[j%n])){
t++;
}
}
}
int k = c/t;
int t0 = c%t;
if(t0!=0){
for (int i = 0; i < 2*b-2; i++) {
for (int j = 0; j <= i; j++) {
if(word.equals(strs[j%n])){
t0--;
if(t0==0){
System.out.println(k*(2*b-2)+i);
}
}
}
}
}else {
System.out.println(k*(2*b-2));
}
}
}