拼多多笔试-基础平台研发-123题解

拼多多笔试,基础平台研发,4个过了仨

1.谁喝到最后一个复制可乐,比较简单

2.世界杯选球王,输入那个地方不太熟搞了半天,然后题目又理解了好半天,基本上理解了就写了个暴力比较,过了

3.N个货物需要装几个车,重量从大到小贪心的选择,判断是否能连续装载,本来AC的还有错误尴尬后来改了下或许对了吧

4.手机号变靓号,我还要再想想。。。

看你们说的其他岗位的题,跟我的还不太一样?

————————————————————分界线————————————————

第一次写帖子,不太熟编辑格式尴尬

写的不太好的地方,多多指正了

第一题代码

import java.util.Scanner;

public class Main_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] names = {"Alice", "Bob", "Cathy", "Dave"};
        int curr = 0;
        int fuzhi = 1;
        while(true){
            int judge = 0;
            for(int i = 0; i < 4; i++){
                curr += fuzhi;
                if(curr >= n){
                    System.out.println(names[i]);
                    judge = 1;
                    break;
                }
            }
            if(judge == 1){
                break;
            }
            fuzhi *= 2;
        }
    }
}

第二题代码

import java.util.Scanner;
public class Main_2 {
    public static void main(String[] args) {
        // 输入
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();
        String[] strs = new String[m];
        for(int i = 0; i < m; i++){
            strs[i] = sc.nextLine();
        }
        // 两两球星比较,matrix[i][j] > 0 , 表示i球星强于j球星
        int[][] matrix = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = i+1; j < n; j++){
                int cishu = 0;
                for(int k = 0; k < m; k++){
                    if(strs[k].charAt(i) > strs[k].charAt(j)){
                        cishu--;
                    }else if(strs[k].charAt(i) < strs[k].charAt(j)){
                        cishu++;
                    }
                }
                matrix[i][j] = cishu;
                matrix[j][i] = -cishu;
            }
            matrix[i][i] = 1;
        }
        // 判断是否有球王,i球星大于每一个球星
        boolean exist = false;
        for(int i = 0; i < n; i++){
            boolean judge = true;
            for(int j = 0; j < n; j++){
                if( matrix[i][j] <= 0){
                    judge = false;
                }
            }
            // 存在球王
            if(judge == true){
                System.out.println(i);
                exist = true;
                break;
            }
        }
        // 不存在球王
        if(exist == false){
            System.out.println(-1);
        }
    }
}

第三题代码,原答案,AC了但有错误

import java.util.Arrays;
import java.util.Scanner;
public class Main_3 {
    public static void main(String[] args) {
        // 输入
        // 直接输入一行不太会,按说先输入N,再输入N个数多好
        // 然后我就把它当字符串,再转换成数组了
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String[] strlist = str.split(" ");
            int len = strlist.length;
            int[] nums = new int[len];
            for(int i = 0; i < strlist.length; i++){
                nums[i] = Integer.parseInt(strlist[i]);
            }
            // 排序
            Arrays.sort(nums);
            // 判断用几个车
            int carNum = 0;
            int[] check = new int[len];
            for(int i = 0; i < len ; i++){
                if(check[i] == 0){
                    // 一个车先装一个货物
                    check[i] = -1;
                    carNum++;
                    // 一个车总共装三个货物,只有三个100重量装一个车的情况
                    if(i+2 < len && nums[i] == 100 && nums[i+2] == 100){
                        check[i+1] = -1;
                        check[i+2] = -1;
                    }else {
                        // 一个车总共装两个货物
                        int temp = 300 - nums[i];
                        for (int j = len - 1; j > i; j--) {
                            if (check[j] == 0 && nums[j] <= temp) {
                                temp = temp - nums[j];
                                check[j] = -1;  // 应该可以直接break,当时这样写了就这样吧
                            }
                        }
                    }
                }
            }
            System.out.println(carNum);
        }
    }
}

第三题代码,修改

import java.util.Arrays;
import java.util.Scanner;
public class Main_3_1 {
    public static void main(String[] args) {
        // 输入
        // 直接输入一行不太会,按说先输入N,再输入N个数多好
        // 然后我就把它当字符串,再转换成数组了
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String[] strlist = str.split(" ");
            int len = strlist.length;
            int[] nums = new int[len];
            for(int i = 0; i < strlist.length; i++){
                nums[i] = Integer.parseInt(strlist[i]);
            }
            // 排序
            Arrays.sort(nums);
            // 判断用几个车
            int carNum = 0;
            int[] check = new int[len];
            for(int i = len-1; i >= 0 ; i--){  // 改了下,从大到小装货物
                if(check[i] == 0){
                    // 一个车先装一个货物
                    check[i] = -1;
                    carNum++;
                    // 从大到小判断是否 继续装货物
                    int temp = 300 - nums[i];
                    for (int j = len - 1; j >= 0; j--) {
                        if (check[j] == 0 && nums[j] <= temp) {
                            temp = temp - nums[j];
                            check[j] = -1;
                        }
                    }
                }
            }
            System.out.println(carNum);
        }
    }
}

}
测试用例:
130 140 150 160
2
130 140 150 160 100 100 100
3
100 100 100 100 100 100 200 200 200
4

#笔试题目##拼多多##题解##秋招##内推#
全部评论
运货的 #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() {     int L=0,w,wsum=0;     vector<int> weight;     while (cin>>w)         weight.push_back(w);          sort(weight.begin(), weight.end());     int left = 0, right = weight.size() - 1;     int count = 0;     for (int i = 0; i < weight.size(); ++i)         if (weight[i] == 100)             ++count;     if (count>=3)     {         L = count / 3;         left = L * 3;     }     while (left < right)     {         if (weight[left] + weight[right]>300)         {             --right;             ++L;         }         else         {             ++left;             --right;             ++L;         }              }     if (left == right)         ++L;     cout << L << endl;     system("pause");     return 0; }
点赞 回复 分享
发布于 2018-07-22 22:22
票选球王思路: 定义两个二维char型数组input和output(类型为vector<vector<char>>),一个二维数组input用来存放输入的字符串,然后将二维字符串数组行列倒置存入第二个数组output中,则output字符串数组中的每一个字符串str为每个选票人队每个候选球星的评定等级。将每个字符串str从小到大排序(即按字母顺序'a'在最前,'z'在最后,str最小即等级最高),比较每个str的大小,如果仅有唯一一个最小的字符串,则其对应的候选球星为球王,返回字符串索引;如果最小的字符串不止一个,则不存在球王,返回-1。 票选球王代码链接:https://blog.csdn.net/lizhentao0707/article/details/81159859
点赞 回复 分享
发布于 2018-07-23 09:30
有木有大佬有拍下来的原题,求分享
点赞 回复 分享
发布于 2018-07-22 22:20
膜一下java大佬
点赞 回复 分享
发布于 2018-07-22 22:16
飞哥还是6呀
点赞 回复 分享
发布于 2018-07-22 22:14
感觉这次题目有坑。。 100 100 100 100 100 100 200 200 200  如果第三题是这么样子的话,你的输出是5.。。应该是4
点赞 回复 分享
发布于 2018-07-22 22:00
看来我还是整理下代码吧,做题的时候写的有点乱。。。
点赞 回复 分享
发布于 2018-07-22 21:28
求问第二题我过了80,也是暴力,不知道是不是有什么边界条件需要注意?
点赞 回复 分享
发布于 2018-07-22 21:26
哥们代码分享一下
点赞 回复 分享
发布于 2018-07-22 21:23

相关推荐

哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务