9.14携程笔试后端

第一题算面积的
import java.util.Scanner;

public class first {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(),k = sc.nextInt();
        double v = n+0D;
        if(k==1){
            System.out.printf("%.2f%n",v*v/2);
            return;
        }
        int x = k>>1;
        int y = k-x;
        double res = (v*v)/((x+1)*(y+1));
        System.out.printf("%.2f%n",res);
    }

}
2.ab矩阵最小交换次数
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class second {
    static int[][] dist = {{-1,0},{1,0},{0,-1},{0,1}};
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int[][] a = new int[][]{{sc.nextInt(),sc.nextInt()},{sc.nextInt(),sc.nextInt()}};
            int[][] b = new int[][]{{sc.nextInt(),sc.nextInt()},{sc.nextInt(),sc.nextInt()}};
            if(cout(a)!=cout(b)){
                bw.write("-1");
                continue;
            }
            int time = Integer.MAX_VALUE>>1;
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    time = Math.min(getTime(a, b,j,k),time);
                }
            }
            if(time>=Integer.MAX_VALUE>>1)
                bw.write("-1");
            else
                bw.write(Integer.toString(time));
            if(i<t-1) bw.newLine();
        }
        bw.flush();
    }

    private static int cout(int[][] b) {
        int res = 0;
        for (int[] ints : b) {
            for (int anInt : ints) {
                if(anInt==1)res++;
            }
        }
        return res;
    }

    private static int getTime(int[][] a, int[][] b, int x, int y) {
        if(isVaild(a,b))
            return 0;
        if(x<0||x>=2||y<0||y>=2)
            return Integer.MAX_VALUE>>1;
        int res = Integer.MAX_VALUE>>1;
        for (int[] ints : dist) {
            swap(x,y,x+ints[0],y+ints[1],a);
            res = Math.min(res,getTime(a,b,(y==1?x+1:x),(y==1?0:y+1))+1);
            swap(x,y,x+ints[0],y+ints[1],a);
        }
        return res;
    }

    private static void swap(int x, int y, int x1, int y1, int[][] a) {
        if(x1<0||x1>1||y1<0||y1>1)return;
        int k = a[x][y];
        a[x][y] = a[x1][y1];
        a[x1][y1] = k;
    }


    private static boolean isVaild(int[][] a, int[][] b) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if(a[i][j]!=b[i][j])
                    return false;
            }
        }
        return true;
    }
}

3.最大值最小值
import java.io.*;

public class fourth {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(br.readLine());
        for (int i = 0; i < t; i++) {
            int n = Integer.parseInt(br.readLine());
            String[] arr = br.readLine().split(" ");
            String s = br.readLine();
            int rmax = 0,rmin = Integer.MAX_VALUE;
            int bmax = 0,bmin = Integer.MAX_VALUE;
            for (int j = 0; j < n; j++) {
                if(s.charAt(j)=='R'){
                    rmax = Math.max(Integer.parseInt(arr[j]),rmax);
                    rmin = Math.min(Integer.parseInt(arr[j]),rmin);
                }else {
                    bmax = Math.max(Integer.parseInt(arr[j]),bmax);
                    bmin = Math.min(Integer.parseInt(arr[j]),bmin);
                }
            }

            int max = Math.max(rmax - rmin, bmax - bmin);
            max = Math.max(max,rmax-bmin);
            bw.write(Long.toString(max));
            bw.newLine();
        }
        bw.flush();
    }
}

第4题n*n矩阵最大权值
import java.util.Scanner;
public class third {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextInt();
        long u = n*n;int mod = (int)1e9+7;
        long len =( u-(n-1)*4)%mod;
        long res = (((2*u-len+1))%mod*len*2)%mod;
        long le = u - len - 4;
        long prenum = (u-len+5);
        if((le&1)==1){
            prenum = (prenum>>1)%mod;
            res = (res+(prenum*(le%mod)*3)%mod)%mod;
        }else {
            prenum = prenum%mod;
            le = (le>>1)%mod;
        }
        res = (res+20+(prenum*(le%mod)*3)%mod)%mod;
        System.out.println(res);
    }
}



#携程笔试##携程#
全部评论
最后一题时间来不及了,最后少取了个模,只过了20,血亏
点赞 回复
分享
发布于 2022-09-14 21:58 北京
第四题为什么要多加一遍啊?
1 回复
分享
发布于 2022-09-16 22:59 上海
联想
校招火热招聘中
官网直投
同学同花顺尝试一下吗,面试简单不造火箭,我帖子有内推
点赞 回复
分享
发布于 2022-09-14 23:55 浙江
对第四题答案表示疑问,如果le是奇数,prenum就÷2,然后加了结果,那最后怎么还要再加一遍呢,是不是重复了。
点赞 回复
分享
发布于 2022-09-16 21:51 陕西

相关推荐

点赞 10 评论
分享
牛客网
牛客企业服务