3.12美团笔试Java代码
还是太菜,没有一道AC。
- 第一道90%
- 第二道90%
- 第三道看错题了(代码是错的,最后仔细看题才发现,没时间改了)过了36%
- 第四题读题7分钟没写
- 第五题动了动手,发现看不懂题,打扰了。。。
代码写的有点丑。。。
第一题dp:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
char[][] map = new char[2][n];
for (int row = 0;row < 2;row++) {
map[row]= input.next().toCharArray();
}
if (n == 1) {
if (map[0][0] == '.' && map[1][0] == '.')
System.out.println(1);
else
System.out.println(-1);
} else {
int cnt = 0;
boolean flag = true;
for (int col = n - 1;col >= 1;col--) {
if (map[0][col] == '.' && map[1][col] == '.')
cnt += 2;
else if(map[0][col] == 'X' && map[1][col] == 'X') {
cnt = -1;
break;
}
}
if (cnt == 0 || map[0][0] == 'X')
System.out.println(-1);
else
System.out.println(cnt);
}
}
}
第二题暴力:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int x = input.nextInt();
int[] nums = new int[n];
int max = 1;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0;i < n;i++) {
nums[i] = input.nextInt();
Integer initCount = map.getOrDefault(nums[i], 0);
initCount += 1;
map.put(nums[i], initCount);
max = Math.max(initCount, max);
}
map.clear();
for (int i = 0;i < n;i++) {
if (map.containsKey(nums[i]))
continue;
Integer cnt = 1;
for (int j = 0;j < n && cnt + n - j - 1 >= max;j++) {
if (j == i)
continue;
if (nums[i] == nums[j] || (nums[j] | x) == nums[i]) {
cnt++;
}
}
map.put(nums[i], cnt);
max = Math.max(cnt, max);
}
System.out.println(max);
}
}
第三题看错题了,看成了数组的每一个元素都是k的倍数。。。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n,k,L,R;
BigInteger mod = new BigInteger("1000000007");
Scanner input = new Scanner(System.in);
n = input.nextInt();
k = input.nextInt();
L = input.nextInt();
R = input.nextInt();
BigInteger res = new BigInteger("1");
long count = 0;
for (int i = L;i <= R;i++) {
if (i % k == 0)
count++;
}
BigInteger cnt = new BigInteger(String.valueOf(count));
res = cnt.pow(n).mod(mod);
System.out.println(res);
}
}
查看5道真题和解析
