2022.8.30携程笔试总结
p1:给个数交换一次或零次各个上的数字,返回偶数(不满足返回-1)
事后琢磨的代码如下,自己跑没问题。
import java.util.*;
public class p1 {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int [] arr=new int[n+1];
String[] arr2=new String[n+1];
for (int i = 0; i <n ; i++) {
arr[i]=sc.nextInt();
arr2[i]=String.valueOf(arr[i]);
}
for (int i = 0; i <n ; i++){
System.out.println(change(arr2[i]));
}
}
public static int change(String s){
int len=s.length();
if(Integer.valueOf(s)%2==0){
return Integer.valueOf(s);
}
for (int i = 0; i <len ; i++) {
if((s.charAt(i)-'0')%2==0){
//System.out.println((s.charAt(i) - '0'));
s= swap(i,len-1,s);
}
else {
return -1;
}
}
return Integer.valueOf(s);
}
public static String swap(int i,int j,String s){
char temp=s.charAt(i),temp2=s.charAt(j);
StringBuilder strB = new StringBuilder(s);
strB.setCharAt(i,temp2);
strB.setCharAt(j,temp);
return strB.toString();
}
}
p2:y,o,u三种字符,拼成字符串;字符串存在you 加 2分 存在oo加一分 求一组字符能得到的最大的分 做题的时候焦虑的不行,想不出思路。看了一圈大佬的题解,发现有时候就得敢想。思路如下
you
you = min(y, min(o, u))
oo = max(0, o - you - 1)
ans = you * 2 + oo
you = min(y, min(o, u))
oo = max(0, o - you - 1)
ans = you * 2 + oo
自己按照这个思路写的,自己下来跑过了,如果有错希望大佬能指点指点。
import javax.management.StringValueExp;
import java.util.*;
public class p1 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[][] nums = new long[n][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
nums[i][j] = sc.nextLong();
}
}
for (int i = 0; i < n; i++){
System.out.println(fun(nums[i][0], nums[i][1], nums[i][2]));
}
}
public static long fun(long y,long o,long u){
long you =0;
long ans=0;
you=Math.min(y,Math.min(o,u));
ans=2*you+o-1-you;
return ans;
}
}
p3:三色树。 不会做,直接跳过了,题目忘了。正在看其他人的解答。。
p4:平滑值(相邻数的差),具体忘了,也在看解答
查看10道真题和解析