腾讯笔试题——满意度

 

准备参加鹅厂的笔试,练习的笔试题,贴上快排解答和ArrayList俩种解答

 

测试案例:

解答:

1.快排,使用java二维数组

public class test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//n个顾客
        if( n >100000 || n<1){
            System.out.println("您的顾客数不符合规范(1-100000)!");
            return;
        }
        int[][] a = new int[n][2];
        int[][] b = new int[n][2];
        for(int i =0 ;i<n;i++){
            a[i][0] = scanner.nextInt();
            a[i][1] = scanner.nextInt();
            b[i][0]=i;
            b[i][1]=a[i][0]-a[i][1];
        }
        quicksort(b,0,n-1);
        for(int i =0 ;i<n;i++){
            int t = b[i][0];
            b[i]=a[t];
        }
        int num = 0;
        for(int i =0 ;i<n;i++){
            num += (a[i][0]-a[i][1])*(i+1) + a[i][1]*n - a[i][0];
        }
        System.out.println(num);
    }

    private static void quicksort(int[][] arr, int low, int high){
        int index;
        if(low<high) {
            index = sort(arr, low, high);
            quicksort(arr,low,index-1);
            quicksort(arr,index+1,high);
        }
    }

    private static int sort(int[][] arr,int low,int high){
        int temp;
        int[] tmp;
        if(low<high) {
            temp = arr[low][1];
            tmp = arr[low];
            while (low < high) {
                while (arr[high][1] >= temp && low < high) {
                    high--;
                }
                arr[low] = arr[high];
                while (arr[low][1] <= temp && low < high) {
                    low++;
                }
                arr[high] = arr[low];
            }
            arr[low] = tmp;
        }
        return low;
    }
}

2.ArrayList

public class test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<int[]> list = new ArrayList<>();
        for (int i=0; i<n; i++) {
            int b[] = new int[2];
            b[0] = in.nextInt();
            b[1] = in.nextInt();
            list.add(b);
        }

        int num = 0;
        List<Integer> arr = new ArrayList<>();
        for (int i =0; i<n; i++){
            int a = list.get(i)[0];
            int b = list.get(i)[1];
            arr.add(a-b);
            num += b*n - a;//不变的因素
        }
        //利用集合的工具类排序
        Collections.sort(arr);

        for(int i =0 ;i<n;i++){
            num += arr.get(i)*(n-i);
        }
        System.out.println(num);
    }
}

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值