腾讯2022实习生笔试
1、从上往下读数并排序
60% 不知道哪出了问题,有无大佬看看
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution029 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = sc.nextLine();
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < str[0].length(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append(str[j].charAt(i));
}
if (!sb.toString().equals("")) {
list.add(Integer.parseInt(sb.toString()));
}
}
list.sort(Integer::compareTo);
for (int i = 0; i < list.size(); i++) {
if (i != list.size() - 1) {
System.out.print(list.get(i) + " ");
} else if (i == list.size() - 1) {
System.out.println(list.get(i));
}
}
}
} 2、删除非质数下标输出最后元素
AC
public int getNumber(int[] a) {
// write code here
List<Integer> list = new ArrayList<>();
list.add(0);
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
List<Integer> temp = new ArrayList<>();
while (temp.size() != 1) {
temp.clear();
for (int i = 1; i < list.size(); i++) {
if (isZ(i)) {
temp.add(list.get(i));
}
}
list = new ArrayList<>(temp);
list.add(0, 0);
}
return temp.get(0);
}
private boolean isZ(int i) {
if (i == 1) return false;
if (i == 2) return true;
for (int j = 2; j <= i / j; j++) {
if (i % j == 0) return false;
}
return true;
} 3、士兵分组后进攻防守值最小差
AC
import java.util.Scanner;
public class Solution031 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String str = sc.nextLine();
long w = 0;// 攻击
long v = 0; // 防守
// str 0表示 攻击, 1表示防守
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
v += i + 1;
}
}
long res = Integer.MAX_VALUE;
for (int i = 0; i <= str.length(); i++) {
if (i > 0 && str.charAt(i - 1) == '0') {
w = w + i;
}
if (i > 0 && str.charAt(i - 1) == '1') {
v = v - i;
}
res = Math.min(res, Math.abs(w - v));
}
System.out.println(res);
}
} 第4题改改再贴代码,第5题没看
4、链表环最小字典序
5、股票最大持有余额
#腾讯笔试##笔试题目##实习##Java##腾讯#