携程4.15笔试 AK,今天这笔试有点简单的!
我为了快速过题,用的方法都比较暴力,基本上是第一感觉是啥就写啥,没太细想优化方法,大佬们要是有什么好的方法可以在评论区交流一下
第一题题解:
第一题直接用一个2*2的矩阵过一遍就行了,定义y o u三个变量,如果出现就设为真,最后三个都为真,答案加一即可。
第一题代码:
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] cs = new char[n][m];
for(int i = 0;i < n;i++){
String s = sc.next();
cs[i] = s.toCharArray();
}
int ans = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
boolean y = false,u = false,o = false;
for(int k = i;k < Math.min(i + 2,n); k++){
for(int l = j;l < Math.min(j + 2,m);l++){
if(cs[k][l] == 'y') {
y = true;
}
else if(cs[k][l] == 'o') {
o = true;
}
else if(cs[k][l] == 'u') {
u = true;
}
}
}
if(y && o && u) {
ans++;
}
}
}
System.out.println(ans);
}
}
第二题题解:
第二题其实直接贪心就好,我的第一直觉是中间那两个数肯定是最小公倍数最大的组合,但是有特殊情况,因为奇数和偶数不太一样,所以要分情况讨论,如果n为奇数,那最大的那两个就是n / 2和n / 2 + 1,但是当n为偶数时,情况有点特殊,当n为偶数时,又分为n / 2为奇数和n / 2为偶数两种情况,n / 2为偶数的话,那n / 2 - 1和n / 2 + 1是两个奇数,他们两个的最小公倍数是最大的,当n / 2为奇数时,n / 2 - 1和 n / 2 + 1是两个偶数,他们两个的最小公倍数比n / 2 - 2 和 n / 2 + 2要小,所以最终结果应该取n / 2 - 2和n / 2 + 2。
第二题代码:
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0;i < t;i++){
long n = sc.nextLong();
if(n == 2){
System.out.println(1 + " " + 1);
continue;
}
if(n % 2 == 0){
if((n / 2) % 2 == 0){
System.out.println((n / 2 - 1) + " " + (n / 2 + 1));
}else {
System.out.println((n / 2 - 2) + " " + (n / 2 + 2));
}
}else{
System.out.println((n / 2) + " " + (n / 2 + 1));
}
}
}
}
第三题题解:
第三题也比较简单,直接暴力dfs找路径就好了,如果二进制值在[ l , r ]范围内,就给答案+1。
import java.util.*;
public class Main3 {
static int[] w;
static int n;
static long l,r;
static long ans;
static Map<Integer, List<Integer>> map = new HashMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
l = sc.nextLong();
r = sc.nextLong();
String s = sc.next();
w = new int[n + 1];
for(int i = 0;i < n;i++){
w[i + 1] = s.charAt(i) - '0';
}
for(int i = 0;i < n - 1;i++){
int u = sc.nextInt(),v = sc.nextInt();
List<Integer> list1 = map.getOrDefault(u,new ArrayList<>());
List<Integer> list2 = map.getOrDefault(v,new ArrayList<>());
list1.add(v);
list2.add(u);
map.put(u,list1);
map.put(v,list2);
}
for(int i = 1;i <= n;i++){
dfs(i,0L,0,new boolean[n + 1]);
}
System.out.println(ans);
}
static void dfs(int i,long cur,int cnt,boolean[] vis){
if(cur > r) {
return ;
}
cur = (cur << 1) | w[i];
if(cur >= l && cur <= r && cnt > 0) {
ans++;
}
List<Integer> list = map.getOrDefault(i,new ArrayList<>());
vis[i] = true;
for(int next:list){
if(!vis[next]){
dfs(next,cur,cnt + 1,vis);
}
}
vis[i] = false;
}
}
第四题代码:
import java.util.Scanner;
public class Main4 {
static int MOD = (int) 1e9+7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] arr = new long[n];
long ans = 0;
for(int i = 0;i < n;i++){
arr[i] = sc.nextLong();
ans += (arr[i] + 1) * arr[i] / 2;
ans %= MOD;
}
for(int k = 1;k < n;k++){
for(int i = k;i < n - k;i++){
for(int j = 1;j <= k;j++){
if(j == k){
ans += Math.min(arr[i - j],arr[i + j]);
ans %= MOD;
}else{
if(arr[i - j] != arr[i + j]) {
break;
}
}
}
}
}
System.out.println(ans);
}
}
4.18更新:进人才池了😭😭😭
#我的实习求职记录#
查看7道真题和解析