小米oj #5 找出旋转有序数列的中间值
描述
给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2]。 假定数列中无重复元素,且数列长度为奇数。 求出旋转数列的中间值。如数列[4,5,6,7,0,1,2]的中间值为4。
输入
4,5,6,7,0,1,2
输出
4
输入样例
1
1,2,3
4,5,6,7,0,1,2
12,13,14,5,6,7,8,9,10
输出样例
1
2
4
9
#include <bits/stdc++.h>
using namespace std;
int main()
{
// please write your code here
string str;
while(cin >> str){
istringstream ss(str);
string temp;
vector<int> v;
while(getline(ss,temp,',')){
v.push_back(atoi(temp.c_str()));
}
sort(v.begin(),v.end());
vector<int>::iterator i = v.begin(), j = v.end();
while(true){
if(i == j - 1){
cout << *i << endl;
break;
}
i++;
j--;
}
}
return 0;
}