首页 > 网易笔试
头像
帅惊全球
编辑于 2021-04-19 17:20
+ 关注

网易笔试

选择题10道,每道题4分。
编程题3道,每题20分。
第一题,一个sql语句,id和name,求表中姓名有重复的人的个数。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
if (N != 0) {
List<String> uname = new LinkedList<>();
for (int i = 0; i < N; i++) {
String a = in.next();
String ss = in.next();
uname.add(ss);
}
System.out.println(getSql(uname));
} else
System.out.println(0);
}

private static int getSql(List<String> uname) {
int resInt = 0;
Map<String, Integer> res = new HashMap<>();
for (String s : uname) {
if (!res.containsKey(s)) {
res.put(s, 1);
} else
res.put(s, res.get(s) + 1);
}
for (String s : res.keySet()) {
int res1 = res.get(s);
if (res1 != 1) {
resInt++;
}
}
return resInt;
}
}
第二题,迷宫左手法则多少步能走出迷宫。
左手法则:沿着墙壁走,如果左边没墙壁,就往左走,否则看前面有没有墙壁,没有往前走,否则右面有没有墙壁往右走,否则往后走。
#为墙壁,.为路,S为开始位置,E为结束位置。
public class Main {


static Road road;


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int N = in.nextInt();

for (int i = 0; i < N; i++) {

int L = in.nextInt();

int W = in.nextInt();

char[][] arr = new char[W][L];

int start1 = 0, start2 = 0;

char[] c;

String s;

for (int j = 0; j < W; j++) {

s = in.next();

c = s.toCharArray();

for (int k = 0; k < L; k++) {

arr[j][k] = c[k];

if (arr[j][k] == 'S') {

start1 = j;

start2 = k;

}

}

}

System.out.println(result(arr, start1 ,start2));

}

}


private static int result(char[][] arr, int start1, int start2) {

int i = start1;

int j = start2;

int ans = 0;

if (i == 0) {

road = Road.DOWN ;

} else if (j == 0) {

road = Road.RIGHT ;

} else if (i == arr.length - 1) {

road = Road.UP ;

} else {

road = Road.LEFT ;

}

int[] ij;

while (arr[i][j] != 'E') {

ans++;

if (!wallLeft(i, j, arr)) {

ij = trunLeft(i, j);

} else if (!wallFront (i, j, arr)) {

ij = forward(i, j);

} else if (!wallRight (i, j, arr)) {

ij = trunRight(i, j);

} else {

ij = back(i, j);

}

i = ij[0];

j = ij[1];

}

return ++ans;

}


private static int[] back(int i, int j) {

int[] ij = new int[2];

if (road == Road.UP) {

road = Road.DOWN ;

i++;

} else if (road == Road. DOWN) {

road = Road.UP ;

i--;

} else if (road == Road. LEFT) {

road = Road.RIGHT ;

j++;

} else {

road = Road.LEFT ;

j--;

}

ij[0] = i;

ij[1] = j;

return ij;

}


private static int[] trunRight(int i, int j) {

int[] ij = new int[2];

if (road == Road.UP) {

road = Road.RIGHT ;

j++;

} else if (road == Road. DOWN) {

road = Road.LEFT ;

j--;

} else if (road == Road. LEFT) {

road = Road.UP ;

i--;

} else {

road = Road.DOWN ;

i++;

}

ij[0] = i;

ij[1] = j;

return ij;

}


private static int[] forward(int i, int j) {

int[] ij = new int[2];

if (road == Road.UP) {

i--;

} else if (road == Road. DOWN) {

i++;

} else if (road == Road. LEFT) {

j--;

} else {

j++;

}

ij[0] = i;

ij[1] = j;

return ij;

}


private static int[] trunLeft(int i, int j) {

int[] ij = new int[2];

if (road == Road.UP) {

road = Road.LEFT ;

j--;

} else if (road == Road. DOWN) {

road = Road.RIGHT ;

j++;

} else if (road == Road. LEFT) {

road = Road.DOWN ;

i++;

} else {

road = Road.UP ;

i--;

}

ij[0] = i;

ij[1] = j;

return ij;

}


private static boolean wallRight(int i, int j, char[][] arr) {

if (road == Road.UP) {

return arr[i][j + 1] == '#';

} else if (road == Road. DOWN) {

return arr[i][j - 1] == '#';

} else if (road == Road. RIGHT) {

return arr[i + 1][j] == '#';

} else

return arr[i - 1][j] == '#';

}


private static boolean wallFront(int i, int j, char[][] arr) {

if (road == Road.UP) {

return arr[i - 1][j] == '#';

} else if (road == Road. DOWN) {

return arr[i + 1][j] == '#';

} else if (road == Road. RIGHT) {

return arr[i][j + 1] == '#';

} else

return arr[i][j - 1] == '#';

}


private static boolean wallLeft(int i, int j, char[][] arr) {

if (road == Road.UP) {

return arr[i][j - 1] == '#';

} else if (road == Road. DOWN) {

return arr[i][j + 1] == '#';

} else if (road == Road. RIGHT) {

return arr[i - 1][j] == '#';

} else

return arr[i + 1][j] == '#';

}

}


enum Road {

UP, DOWN ,LEFT, RIGHT;

}

第二题抠细节扣的我脑壳疼,结果没在结束之前扣完,结束十多分钟后才把两个测试用例通过,但我觉得问题不大。
第三题,没时间做。
求中位数,如果得分等于中位数,则得到奖励,否则不得奖励,求有多少个人得到了奖励。

全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐