OPPO 2019校園招聘C/C++開發工程師(手機方向) 筆試程式設計題-2018.09.10
阿新 • • 發佈:2019-01-31
思路:
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
思路:
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;
}