您的位置:首页 > 其它

输出字符串中的数字 并进行排序

2015-04-25 16:12 232 查看
#include<iostream>
#include<stdlib.h>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
void quickSort(vector<int>& s, int low, int high)
{
if (low< high)
{
int i = low, j = high, x = s[low];
while (i < j)
{
while (i < j && s[j] >= x)
j--;
if (i < j)
s[i++] = s[j];
while (i < j && s[i]< x)
i++;
if (i < j)
s[j--] = s[i];
}
s[i] = x;
quickSort(s, low, i - 1);
quickSort(s, i + 1, high);
}
}
int main(){
vector<int> a;
string str;
int b = 0;
int k = 0;
getline(cin, str);
int flag = 0;
for (int i = 0; i < str.size(); i++){
if (str[i] >= '0'&&str[i] <= '9')//找出第一个是数字后执行后面的
flag = 1;
if (flag){
if (str[i] >= '0'&&str[i] <= '9'){
b = b * 10 + str[i] - '0';
}
else{
if ((str[i + 1] >= '0'&&str[i + 1] <= '9'))
{
a.push_back(b);
b = 0;
}
}
}
}
a.push_back(b);
for (int i = 0; i<a.size(); i++)
cout << a[i] << " ";
cout << endl;
quickSort(a, 0, a.size() - 1);
for (int i = a.size() - 1; i >= 0; i--)
cout << a[i] << " ";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐