OPPO 2019校园招聘C/C++开发工程师(手机方向) 笔试编程题-2018.09.10

版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82596544

1
思路:
1. 分割字符串,提取数字
2. multimap存储
3. 遍历找到相应结果输出

#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector<int> arr;
int main()
{
    string str;
    cin >> str;
    if(str == "")
        return 0;
    int x1 = 0;
    int x2 = 0;
    int int_temp;
    string stemp;
    while (true){
        x2 = str.find(',', x1);//返回首次匹配的逗号的下标
        if(x2 == -1)
            break;
        stemp = str.substr(x1, x2 - x1);//截取从字符串str中第x1位开始的长度为(x2-x1)的字符串
        int_temp = atoi(stemp.c_str());
        arr.push_back(int_temp);
        x1 = x2 + 1;//更改下次查询起始位置
    }
    stemp = str.substr(x1, str.size()-x1);
    int_temp = atoi(stemp.c_str());
    arr.push_back(int_temp);

    multimap<int,int> myMultimap;
    for(int i=0; i<arr.size(); i++){
        myMultimap.insert( pair<int, int>(arr[i], i+1) );
    }

    int k;
    cin >> k;
    int count = 0;
    for(auto itemp = myMultimap.rbegin(); itemp!=myMultimap.rend(); itemp++){
        ++count;
        if(count == k)
            cout << itemp->second << endl;
    }
    return 0;
}
//1024,3,64,4,64,41,238 5

2-1.png
2-2.png
思路:
1.题目要求二叉排序树转换为双链表
2.但是,我没有看懂输入的样例。。。。。。
3.直接进行分割字符串,提取数字,set存储去重,顺序输出
4.没有AC,过了80%,不知道问题在哪儿

#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main()
{
    string str;
    while(getline(cin, str)){
        set<int> mySet;
        if(str == "")
            return 0;
        int x1 = 0;
        int x2 = 0;
        int int_temp;
        string stemp;
        while (true){
            x2 = str.find(' ', x1);//返回首次匹配的空格的下标
            if(x2 == -1)
                break;
            stemp = str.substr(x1, x2 - x1);//截取从字符串str中第x1位开始的长度为(x2-x1)的字符串
            int_temp = atoi(stemp.c_str());
            mySet.insert(int_temp);
            x1 = x2 + 1;//更改下次查询起始位置
        }
        stemp = str.substr(x1, str.size()-x1);
        int_temp = atoi(stemp.c_str());
        mySet.insert(int_temp);
        int len = mySet.size();

        if(len == 0)
            return 0;

        auto it=mySet.begin();
        for(int i=0; i<len-1; i++){
            cout << *it << " ";
            it++;
        }
        cout << *mySet.rbegin();
    }
    //getline(cin, str);
    //4 2 3 4 5 9 7 6
    return 0;

}

猜你喜欢

转载自blog.csdn.net/whl_program/article/details/82596544