hdu 1106 排序
http://acm.hdu.edu.cn/showproblem.php?pid=1106
问题描述:字符串分段处理成多个数字再排序
stringstream & getline又熟练了一下
思路
用’ ‘(空格)把字符一段一段切开,然后stringstream读取每段的字符,atoi(s.c_str())字符转换成数值就可以了,用向量vector真方便!
参考代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long ll;
string s;
vector<int>v;
vector<int>::iterator it;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
while(getline(cin,s)){
v.clear();
for(int i = 0; i < s.size(); ++ i)
if(s[i] == '5') s[i] = ' ';
stringstream ss(s);
while(ss >> s){//ss读出到s中
int x = atoi(s.c_str());
v.push_back(x);
}
sort(v.begin(),v.end());
for(it = v.begin(); it!=v.end()-1;++it)
cout<<*it<<" ";
cout<<*it<<endl;
}
return 0;
}
- 加粗
Ctrl + B
- 斜体
Ctrl + I
- 引用
Ctrl + Q
- 插入链接
Ctrl + L
- 插入代码
Ctrl + K
- 插入图片
Ctrl + G
- 提升标题
Ctrl + H
- 有序列表
Ctrl + O
- 无序列表
Ctrl + U
- 横线
Ctrl + R
- 撤销
Ctrl + Z
- 重做
Ctrl + Y