思路:
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;
}