阿里笔试8.19
第一题:两个数组按照从前端依次选取的方式是否能拼接成相应数组。递归实现代码如下所示:
import java.util.Scanner;
public class Day_2020_8_19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入测试数据
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
// 输入数组个数
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
int[] b = new int[m];
// 输入数组a
for (int j = 0; j < n; j++) {
a[j] = sc.nextInt();
}
// 输入数组b
for (int k = 0; k < m; k++) {
b[k] = sc.nextInt();
}
int[] c = new int[n+m];
// 处理逻辑
boolean possible = isPossible(a, 0, b, 0, c, 0);
if (possible) {
System.out.println("possible");
} else {
System.out.println("no possible");
}
}
}
public static boolean isPossible(int[] a, int ta, int[] b, int tb,
int[] c, int tc) {
int aLen = a.length;
int bLen = b.length;
int cLen = c.length;
if (ta==aLen) {
while (tc!=cLen && tb!=bLen) {
if (c[tc]!=b[tb]) {
return false;
}
}
} else if (tb==bLen) {
while (tc!=cLen && ta!=aLen) {
if (c[tc]!=a[ta]) {
return false;
}
}
} else {
if (a[ta]==c[tc] && b[tb]==c[tc]) {
return isPossible(a, ta+1, b, tb, c, tc+1)
|| isPossible(a, ta, b, tb+1, c, tc+1);
} else if (a[ta]==c[tc]) {
return isPossible(a, ta+1, b, tb, c, tc+1);
} else if (b[tb]==c[tc]) {
return isPossible(a, ta, b, tb+1, c, tc+1);
} else {
return false;
}
}
return true;
}
} 第二题:求k个连续队员中最大体力和的最小个人体力,代码如下所示:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// 输入正整数n(数组个数)
int n = sc.nextInt();
// 输入对应每个队员能力值
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int k=1; k<=a.length; k++) {
int i = maxMin(a, k);
System.out.print(i + " ");
}
}
}
public static int maxMin(int[] a, int k) {
int length = a.length;
int max = Integer.MIN_VALUE;
int index = 0;
for (int i = 0; i < length-k+1; i++) {
int result = 0;
for (int j=i; j< i+k+1; j++) {
result = result + a[j];
}
if (result>max) {
max = result;
index = i;
}
}
int min = a[index];
for (int i= index; i< index+k+1; i++){
if (a[i] < min) {
min = a[i];
}
}
return min;
}
} #笔试题目##阿里巴巴#总结:这两道题是基础题,但是也是非常容易出错的题,因为你可能考虑的不全面,所以我贴出来和大家交流一下。有不对的地方请指正。
快手公司福利 1244人发布