【stringstream&getline】hdu 1106 排序

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值