腾讯9.20笔试题,求第三题解答
看上去都不难,可是就是AC不了,可能自己太暴力了。
100,10,0,60,20
求第三题思路
附我的代码如下:
第一题,企鹅号码
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
for (int i = 0; i < t; i++){
int x = Integer.parseInt(sc.nextLine());
String str = sc.nextLine();
if (isNum(str))
System.out.println("YES");
else
System.out.println("NO");
}
}
private static boolean isNum(String s){
if (s.length() == 11 && s.charAt(0) == '8')
return true;
if (s.length() < 11)
return false;
int len = s.length();
String tmp = s.substring(0, len-10);
if (tmp.contains("8"))
return true;
return false;
}
}
第二题,最短用时
import java.util.*;
public class Main2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++){
String[] s = sc.nextLine().split(" ");
int x = Integer.parseInt(s[0]);
int y = Integer.parseInt(s[1]);
for (int j = 0; j < x; j++){
list.add(y);
}
}
Collections.sort(list);
int res = Integer.MIN_VALUE;
int len = list.size();
for (int i = 0; i < len/2; i++){
int sum = list.get(i) + list.get(len-i-1);
res = Math.max(sum, res);
}
System.out.println(res);
}
}
第三题,战斗力平均
public class Main3 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
for (int i = 0; i < t; i++){
int n = Integer.parseInt(sc.nextLine());
String[] s = sc.nextLine().split(" ");
int[] x = new int[s.length];
for (int j = 0; j < x.length; j++){
x[j] = Integer.parseInt(s[j]);
}
fun(n, x);
}
}
private static void fun(int n, int[] x){
Arrays.sort(x);
int sum1 = 0;
int sum2 = 0;
int i = 0;
int count = 0;
for (; i+1 < n/2; i = i+2){
sum1 += x[i] + x[n-i-1];
sum2 += x[i+1] + x[n-i-2];
count += 2;
}
int num = count * 2;
if (n - num == 3){
if (sum1 < sum2){
sum1 += x[i]+x[i+1];
sum2 += x[i+2];
}else {
sum2 += x[i]+x[i+1];
sum1 += x[i+2];
}
}else if (n - num == 2){
if (sum1 < sum2){
sum1 += x[i+1];
sum2 += x[i];
}else {
sum2 += x[i+1];
sum1 += x[i];
}
}else if (n - num == 1) {
if (sum1 < sum2)
sum1 += x[i];
else
sum2 += x[i];
}
if (sum1 < sum2)
System.out.println(sum1 + " " + sum2);
else
System.out.println(sum2 + " " + sum1);
}
}
第四题,找最小数x,并减x
import java.util.Arrays;
import java.util.Scanner;
public class Main4 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split(" ");
int n = Integer.parseInt(s[0]);
int k = Integer.parseInt(s[1]);
int[] arr = new int[n];
String[] str = sc.nextLine().split(" ");
for (int i = 0; i < str.length; i++){
arr[i] = Integer.parseInt(str[i]);
}
Arrays.sort(arr);
for (int i = 0; i < k; i++){
int x = findMin(arr);
System.out.println(x);
for (int j = 0; j < arr.length; j++){
if (arr[j] != 0)
arr[j] -= x;
}
}
}
public static int findMin(int[] arr){
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++){
if (arr[i] != 0 && arr[i]<min){
min = arr[i];
break;
}
}
if (min != Integer.MAX_VALUE)
return min;
else
return 0;
}
}
第五题,异或
import java.util.Scanner;
public class Main5 {
public static void main(String[] args){
// System.out.println(or(5,11));
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String[] sa = sc.nextLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++){
a[i] = Integer.parseInt(sa[i]);
}
String[] sb = sc.nextLine().split(" ");
int[] b = new int[n];
for (int i = 0; i < n; i++){
b[i] = Integer.parseInt(sb[i]);
}
int res = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
res = or(res, a[i]+b[j]);
}
}
System.out.println(res);
}
private static int or(int a, int b){
int res = a^b;
return res;
}
}
#笔试题目##腾讯#
