STL-istringstream函数

来源:互联网 发布:天猫运营数据分析表格 编辑:程序博客网 时间:2024/06/12 22:15

排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52225 Accepted Submission(s): 15272

Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。

Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。

Sample Input
0051231232050775

Sample Output
0 77 12312320

Source
POJ

Recommend
Eddy
//灵活使用了istringstream函数和vectorSTL

#include<sstream>#include<vector>#include<cstdlib>#include<algorithm>using namespace std;int main(){    string str;    while(cin>>str)    {        vector<string> v1;        vector<int> v2;        for(int i=0;i!=str.size();++i)        {            if(str[i]=='5')                str[i]=' ';        }    istringstream in(str);//主要对于有空格的预处理    string temp;    while(in>>temp)        v1.push_back(temp);    for(auto b=v1.cbegin();b!=v1.cend();++b)    {        int temp=atoi(b->c_str());        v2.push_back(temp);    }    sort(v2.begin(),v2.end());    auto b=v2.cbegin();    for(;b!=v2.cend()-1;++b)        cout<<*b<<" ";    cout<<*b<<endl;    }    return 0;}

//下面是strtok函数的AC代码

#include<iostream>#include<cstring>#include<algorithm>using namespace std;int main(){    char a[1001],*p,*fuck="5",*j;    int y[1000],i,n;    while(cin>>a)    {        p=a;i=0;        p=strtok(a,fuck);        while(p!=NULL)        {            j=p;            p=strtok(NULL,fuck);            y[i]=atoi(j);            i++;        }        sort(y,y+i);        cout<<y[0];        for(n=1;n<i;n++)            cout<<" "<<y[n];        cout<<endl;    }    return 0;}
使用了逐个读入字符的老式方法,因为看错题目,以为只有一组数据,但是思路在这。#include <iostream>#include<algorithm>#include<stdlib.h>#include <string.h>using namespace std;int main(){   char ch;   char d[1000];   int b[1000];   int c=0,e=0;   int i,j,flag4;   char flag1='#';   bool  flag2=true,flag3=false;  while(scanf("%c",&ch)!=EOF)  {  if(ch=='\n'||ch=='\r')  {   if(e>=0)  {   if(flag3&&(flag1=='#'||flag1=='5'))b[c++]=0;//纯0  else {  d[e]='\0';  int len=strlen(d);  if(len){b[c++]=atoi(d);e=0;}      }  }  sort(b,b+c);   for(i=0;i<c;i++)    printf("%d ",b[i]);putchar(10);  c=0;e=0;flag1='#';flag3=false;  }  else if(ch=='0')  {  if(flag1=='5'||flag1=='#'){flag3=true;continue;}//首位为0  else d[e++]=ch;//不纯0  }  else if(ch=='5')  {  if(flag3&&(flag1=='#'||flag1=='5')){b[c++]=0;flag3=false;}//纯0  else {  d[e]='\0';  int len=strlen(d);  if(len){b[c++]=atoi(d);e=0;}       }  }  else   {if(flag1=='5') flag3=false;    d[e++]=ch;  }  flag1=ch;  }    return 0;}
0 0
原创粉丝点击