网易2022秋季校园招聘-通用技术A卷-0918
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网易2022秋季校园招聘-通用技术A卷-0918相关的知识,希望对你有一定的参考价值。
https://gitee.com/shentuzhigang/algorithm/tree/master/exam-netease/exam-netease-20210918
编程题
第一题
解决方案
JAVA
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-09-18 18:03
*/
public class ExamNetEase2021091801 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String str = String.valueOf(n);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != '0' && n % (str.charAt(i) - '0') == 0) {
count++;
}
}
System.out.println(count);
}
}
第二题
解决方案
JAVA
71%
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-09-18 18:03
*/
public class ExamNetEase2021091802 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] strings = str.split(" ");
str = strings[0];
int n = Integer.parseInt(strings[1]);
int[] a = new int[str.length() - 1];
long count = 0;
long sum = 0;
long max = 0;
for (int i = 1; i < str.length(); i++) {
int i1 = Math.abs(str.charAt(i) - str.charAt(i - 1));
a[i - 1] = Math.min(26 - i1, i1);
count += a[i - 1];
sum += a[i - 1];
if (i - 1 - n >= 0) {
count -= a[i - 1 - n];
max = Math.max(max, count);
}
}
if (a.length > n) {
System.out.println(Math.min(sum + str.length() - max + n, sum + str.length()));
} else {
System.out.println(sum + str.length());
}
}
}
第三题
解决方案
JAVA
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-09-18 18:03
*/
public class ExamNetEase2021091803 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n0 = scanner.nextLong();
long n = n0;
if (n == 0) {
System.out.println(-1);
return;
}
int count1 = 0, count2 = 0, num = 0;
while (n != 0) {
if (n % 2 == 1) {
count1++;
}
num++;
n /= 2;
}
long m = (long) (Math.pow(2, num) - n0);
while (m != 0) {
if (m % 2 == 1) {
count2++;
}
m /= 2;
}
System.out.println(Math.min(count1, count2 + 1));
}
}
第四题
解决方案
JAVA
版本一
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-09-18 18:03
*/
public class ExamNetEase2021091804 {
private static int n;
private static int a;
private static int b;
private static int[][] ans;
private static boolean flag = false;
private static int[][] pos = new int[2][2];
private static char[][] map;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = scanner.nextInt();
b = scanner.nextInt();
map = new char[n][n];
ans = new int[n][n];
pos[0][0] = -1;
pos[0][1] = -1;
pos[1][0] = -1;
pos[1][1] = -1;
int p = 0;
scanner.nextLine();
for (int i = 0; i < n; i++) {
map[i] = scanner.nextLine().toCharArray();
for (int j = 0; j < n; j++) {
if (map[i][j] == '*') {
pos[p][0] = i;
pos[p][1] = j;
flag = true;
}
ans[i][j] = Integer.MAX_VALUE;
}
}
PriorityQueue<Node> q = new PriorityQueue<>(new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.count - o2.count;
}
});
Node node = new Node();
node.x = 0;
node.y = 0;
node.count = 0;
q.add(node);
while (q.size() > 0) {
node = q.poll();
if (node.x >= n || node.y >= n || node.x < 0 || node.y < 0 || node.count > ans[n - 1][n - 1]) {
continue;
}
if (ans[node.x][node.y] > node.count) {
ans[node.x][node.y] = node.count;
} else {
return;
}
int c1 = 0;
if (map[node.x][node.y] == '#') {
c1 = a;
} else if (map[node.x][node.y] == '*') {
Node node1 = new Node();
if (node.x == pos[0][0] && node.y == pos[0][1]) {
node1.x = pos[1][0];
node1.y = pos[1][1];
} else {
node1.x = pos[0][0];
node1.y = pos[0][1];
}
node1.count = node.count + b;
q.add(node1);
}
dfs(q, node.x + 1, node.y, node.count + c1);
dfs(q, node.x, node.y + 1, node.count + c1);
dfs(q, node.x - 1, node.y, node.count + c1);
dfs(q, node.x, node.y - 1, node.count + c1);
}
System.out.println(ans[n - 1][n - 1]);
}
public static void dfs(PriorityQueue<Node> q, int x, int y, int count) {
Node node1 = new Node();
node1.x = x;
node1.y = y;
node1.count = count;
q.add(node1);
}
static class Node {
int x;
int y;
int count;
}
}
版本二
超时
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-09-18 18:03
*/
public class ExamNetEase2021091804_2_TIMEOUT {
private static int n;
private static int a;
private static int b;
private static int[][] ans;
private static boolean flag = false;
private static int[][] pos = new int[2][2];
private static char[][] map;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = scanner.nextInt();
b = scanner.nextInt();
map = new char[n][n];
ans = new int[n][n];
pos[0][0] = -1;
pos[0][1] = -1;
pos[1][0] = -1;
pos[1][1] = -1;
int p = 0;
scanner.nextLine();
for (int i = 0; i < n; i++) {
map[i] = scanner.nextLine().toCharArray();
for (int j = 0; j < n; j++) {
if (map[i][j] == '*') {
pos[p][0] = i;
pos[p][1] = j;
flag = true;
}
ans[i][j] = Integer.MAX_VALUE;
}
}
dfs(0, 0, 0);
System.out.println(ans[n - 1][n - 1]);
}
public static void dfs(int x, int y, int count) {
if (x >= n || y >= n || x < 0 || y < 0 || count > ans[n - 1][n - 1]) {
return;
}
if (ans[x][y] > count) {
ans[x][y] = count;
} else {
return;
}
int c1 = 0;
if (map[x][y] == '#') {
c1 = a;
} else if (map[x][y] == '*') {
if (x == pos[0][0] && y == pos[0][1]) {
dfs(pos[1][0米哈游2023秋季招聘正式开始~提前批有机会免笔试!