2021-09-01小米软开笔试

小米 2021.9.1笔试题
题一 合并有序链表 (题目很简单,但是输入输出贼烦)

#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
using namespace std;

int main()
{
    int m, n;
    string input;
    while (getline(cin, input))
    {
        istringstream ss(input);
        string str;
        vector<int> nums;
        while (getline(ss, str, ','))
        {
            string tmp = str.substr(2);
            nums.push_back(stoi(tmp));
        }
        m = nums[0];
        n = nums[1];
        string A, B;
        vector<int> numA(m+n, 0), numB(n, 0);

        int a = 0;
        cin >> numA[0];
        while (cin.get() == ',')
        {
            cin >> numA[++a];
        }
        a = 0;
        cin >> numB[0];
        while (cin.get() == ',')
        {
            cin >> numB[++a];
        }

        int l = m-1, j = n-1, size = m + n -1;
        while (l >= 0 && j >= 0)
        {
            if (numA[l] > numB[j])
            {
                numA[size--] = numA[l--];
            }
            else
            {
                numA[size--] = numB[j--];
            }
        }
        while (j >= 0)
        {
            numA[size--] = numB[j--];
        }
        for (int l = 0;l < numA.size(); l++)
        {
            cout << numA[l] << " ";
        }
        
    }
 
    return 0;
}

第二题 整数列表去3的倍数

#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<list>
using namespace std;
struct Node {
    int data;
    Node* next;
    Node(int value) { this->data = value; }
};
int main()
{
    int N;
    while (cin >> N)
    {
        list<int> q;
        Node* Tail = new Node(1);
        Tail->next = Tail;
        for (int i = 2; i <= N; i++)
        {
            Node* new_node = new Node(i);
            new_node->next = Tail->next; 
            Tail->next = new_node;
            Tail = new_node;
        }

        Node* node = Tail;
        int step = 3;
        while (node != NULL && node != node->next)
        {
            for (int i = 0; i < step - 1; i++)
            {
                node = node->next;
            }
            Node* del = node->next;
            node->next = del->next;
        }
        cout << node->data << endl;
    }  
 
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值