• 23年秋招笔试算法题:美团蔚来


    美团

    (一)两种糖果礼物盒里装三个

    题目描述:A糖x个,B糖y个,每个礼盒装3个,A、B至少各有一个,最多能装几个礼盒?

    #include 
    #include 
    using namespace std;
    int giftNum(int x, int y) {
        if (x - y > y) return y;
        if (y - x > x) return x;
        int max = 0, min = 0, count = 0, temp = 0;
        x > y ? (max = x, min = y) : (max = y, min = x);
        while (min >= 2 || max >= 2) {
            max = max - 2;
            min = min - 1;        
            count++;
            max > min ? (max = max) : (temp = max, max = min, min = temp);
        }
        return count;
    }
    int gift_num(int x, int y) 
    {
        int max = 0;
        int gift_num = 0;
        if (x > y){
            max = x;
            if (x - y >= y)
                return y;
        }else{
            max = y;
            if (y - x >= x)
                return x;
        }
        for (int i = 0; i < max; i++) 
        {
            if (x > 0 && y > 0) 
            {
                if (!(x == 1 && y == 1)) {
                    x--;
                    y--;
                    if (x > y) {
                        x--;
                        gift_num++;
                    } else {
                        y--;
                        gift_num++;
                    }
                }
            }
        }
        return gift_num;
    }
    
    int main()
    {
        int n = 0;
        cin >> n;
        vector<vector<int>> vv;
        vector<int> v1, v2;
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            v1.push_back(temp);
            cin >> temp;
            v2.push_back(temp);
        }
        vv.push_back(v1);
        vv.push_back(v2);
        for (int i = 0; i < n; i++) {
            cout << vv[0][i] << " " << vv[1][i] << endl;
        }
        cout << giftNum(5,6) << endl;
        cout << giftNum(44, 85) << endl;
        cout << giftNum(9, 49) << endl;
        cout << giftNum(20, 28) << endl;
    
        cout << gift_num(5, 6) << endl;
        cout << gift_num(44, 85) << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    (二)超过一半儿的魔法符文

    题目描述:两行n列,第一行为正面数字,第二行为反面数字,至少要翻动多少次才能是使得其中一面超过一半的数字为相同的数。

    #include 
    #include 
    using namespace std;
    
    int re(){
        int n;
        cin >> n;
        vector<int> v1,v2;
        int max1 = 0, max2 = 0;
        int val1, val2;
        int re = 0;
    
        for(int i = 0; i < n ; i ++){
            int temp = 0;
            cin >> temp;
            v1.push_back(temp);
        }
        for(int i = 0; i < n ; i ++){
            int temp = 0;
            cin >> temp;
            v2.push_back(temp);
        }
    
        for(int i = 0; i < n ; i ++){
            int temp = count(v1.begin(), v1.end(), v1[i]);
            if(temp > max1){
                max1 = temp;
                val1 = v1[i];
            }
        }
        for(int i = 0; i < n ; i ++){
            int temp = count(v2.begin(), v2.end(), v2[i]);
            if(temp > max2){
                max2 = temp;
                val2 = v2[i];
            }
        }
    
    
        if(max1 > (n/2 + 1) || max2 > (n/2 +1)){
            cout << "n/2 + 1:" << (n/2 + 1) << endl;
            cout << 0;
            return 0;
        }
        if(max1 > max2){
            for(int i = 0; i < n ; i ++){
                if(v2[i] == val1 && v1[i] != val1)
                    re++;
                if((max1 + re) > n/2){
                    cout << re;
                    return re;
                }
            }
        }else if(max2 > max1){
            for(int i = 0; i < n ; i ++){
                if(v1[i] == val2 && v2[i] != val2)
                    re++;
                if((max2 + re) > n/2) {
                    cout << re;
                    return re;
                }
            }
        }
    }
    
    
    int main(){
        re();
        return -1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    蔚来

    (一)阶乘结果有几个零?

    https://onlinegdb.com/hBy1lm_22B

    #include 
    using namespace std;
    int main()
    {
    	unsigned int n;
    	unsigned long long factorial = 1;
    	int count = 0;
    	
    	cout << "输入一个整数: ";
    	cin >> n;
    	
    	for(int i = 1; i <=n; ++i)
    		factorial *= i;
    	
    	cout << n << " 的阶乘为:"<< " = " << factorial << endl;
    	while (factorial != 0) {
    		if (factorial % 10 == 0)
    			count++;
    		factorial = factorial / 10;
    	}
    	cout << count << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (二)裁切字串判断回文:最长公共子序列 LCS

    小红拿到了一个字行串。她准备删除其中一段区问,使得剩下的部分回文。
    小红希塑最终得到的字符串长度尽可能大,你能求出这个长度吗?
    输入描述:
    一个字符串,长度不超过2000
    输出描述:
    刪涂区间后留下的最长回文串长度。

    #include 
    #include 
    #include 
    
    using namespace std;
    
    const int MAXN = 2000;
    int temp[MAXN][MAXN];
    
    //先求s的反串reverse,然后求他们的最长的公共子序列,要删除的字符个数就能知道
    //时间复杂度O(N^2)
    
    int getRemoveNumber(string &s1) {
        string s2(s1); //拷贝s1
        reverse(s2.begin(), s2.end());//#include  可以反转 vector、string
        int len = s1.length();
        cout << s1 << endl << s2 << endl;
        memset(temp, 0, sizeof temp);
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < len; ++j) {
                if (s1[i] == s2[j]) //s1[i]是行,s2[j]是列
                    temp[i + 1][j + 1] = temp[i][j] + 1;
                else
                    temp[i + 1][j + 1] = max(temp[i][j + 1], temp[i + 1][j]);//#include   返回最大值
            }
        }
        return len - temp[len][len];
    }
    
    int main() {
        string s;
        while (cin >> s) {
            cout << getRemoveNumber(s) << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    (三)合并数组

    《山洞探验》项目介绍:
    首先,游玩者可以任意规定山门的颜色、由山门进入山洞,该山河一共分为八层,每一层有两家带有颜色的门,这两南门都可以通往下一-层,但游玩者只能从这两家门中选择一房打开。
    如果学玩者当前选择打开的门与上一层打开的门的极色相司,則不需要花费任何代价就可以打开这家门;而如果极色不相同,财游玩省需要花费一定时间进行破泽,破泽完成后才能打开门进入下一层。(特殊的,如果当前是第一层,则
    上一层打开的门的颜色由山门的颜色代营)兰游玩香打开 n家门走出山洞之后,该游乐项目负责人会根据该游玩者所花
    泽的阳进行领发空品,时回林日的奖品裁好为了方世计单,游乐项兰负港人首先会格山们可能出现钩贝种颜色从1~m编号而破泽时间R定义为:上一层打开的门的颜色编号(若当前是第一层,则这里指山门的款色编号)× 当前这一层想安打开的门的颜色编号牛牛和牛妹十分地要山河后的奖品,所以他们请你事先计算一下游玩设项目最少所需要花费的破泽的间是多少,以此来决定白己是否参加。

    
    vector<int> txj(vector<int> a, vector<int> b){
        if(a[0] == b[0] || a[0] == b[1]){
            return a;
        } else{
            int min = 0;
            b[0] > b[1] ? min = b[1] : min = b[0];
            a[1] += a[0] * min;
            a[0] = min;
            return  a;
        }
    }
    
    
    int main(){
        /* 二维数组输入:
         *     列0  列1
         * 行0: 1   3
         * 行1: 2   1
         * 行2: 2   3
         *
         *
         * v1:{1, 3}
         * v1:{2, 1}
         * v1:{2, 3}
         * vv: {{1, 3}, {2, 1}, {2, 3}}
         *
         * vv[行][列]
         * vv[1][0] :2
         */
        vector<vector<int>> vv;
        vector<int> v1,result = {1,0};
        
        int n = 0, m = 0, temp = 0;
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> temp;
            v1.push_back(temp);
            cin >> temp;
            v1.push_back(temp);
            vv.push_back(v1);
            v1.clear();
        }
    
        for(int i= 0; i < vv.size(); i++){
            cout << "|" << result[0] << " " << result[1] << "|" << endl;
            cout << "|" << vv[i][0] << " " << vv[i][1] << "|" << endl;
            result = txj(result,vv[i]);
            cout << result[0] << " " << result[1] << endl;
        }
        cout << result[1] << endl;
        return -1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    // C++输入二维数组
    vector<vector<int>> Cin2DArray(){
        vector<vector<int>> vv;
        vector<int> v;
        int temp = 0, n = 0, m = 0;
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cin >> temp;
                v.push_back(temp);
            }
            vv.push_back(v);
            v.clear();
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout << vv[i][j] << " ";
            }
            cout << endl;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    【第2章 Node.js基础】2.4 Node.js 全局对象(二) process 对象
    Servlet中常见的API详解
    资深架构师必备知识:Netty+MySQL+并发+JVM+多线程
    一步步带你剖析Java中的Reader类
    智慧物流数字孪生怎么样?元宇宙医疗供应商首选广州华锐互动
    微服务框架 SpringCloud微服务架构 10 使用Docker 10.6 容器命令练习
    基于springboot的智慧养老平台设计与实现-计算机毕业设计源码和LW文档
    51-43 DragNUWA,集成文本、图像和轨迹实现视频生成细粒度控制
    陈吉宁经典演讲:平庸与卓越的差别
    C++11 Alias Template(template typedef)化名,Alias,换一个名称
  • 原文地址:https://blog.csdn.net/qq_33177268/article/details/126196993