Topic
Describe:
Implement input a set of integers greater than or equal to 0, according to the order from small to large output, after sorting there are consecutive numbers, only the minimum and maximum number of consecutive number of two.
Topic Category: Sort
Difficulty: Advanced
Operating time limit: 10Sec
Memory Limit: 128MByte
Stage: Pre-employment practice
Input:
A set of integers greater than or equal to 0, regardless of illegal input, separated by commas (",") between the integers, the total length of the input string is less than or equal to 100 bytes.
Output:
The sorted value, separated by a space between the individual integers.
Sample input:
1,4,3,110,2,90,7
Sample output:
1 4 7) 90 110
Code
/* ---------------------------------------* Date: 2015-06-28* sjf0115* title: integer sort * URL: http://ilearning.hwclouds.com/e Xam/showsolution?method=solutionapp&id=4287* Source: Huawei on-Machine-----------------------------------------*/#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace STD; vector<int>Sort (stringSTR) {intSize = Str.size (); vector<int>Vecif(Size = =0){returnVec }//if intnum =0;//Do not consider illegal input for(inti =0; I <= size;++i) {if(i = = Size | | str[i] = =', ') {vec.push_back (num); num =0; }//if Else if(Str[i] >=' 0 '&& Str[i] <=' 9 ') {num = num *Ten+ Str[i]-' 0 '; }//else}//for //SortSort (Vec.begin (), Vec.end ());intCount = Vec.size ();intStart =0; for(inti =1; I <= count;++i) {if(i = = Count | | vec[i]! = vec[i-1]+1){//-1 Mark Delete for(intj = start+1; J <= I-2; ++j) {Vec[j] =-1; }//forstart = i; }//if}//for returnVEC;}intMain () {stringStr vector<int>VecintSize//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin); while(Cin>>STR) {VEC = Sort (str); Size = Vec.size (); for(inti =0; i < size;++i) {if(Vec[i] = =-1){Continue; }//if //First if(i = =0){cout<<vec[i]; }//if Else{cout<<" "<<vec[i]; }//else}//for cout<<endl; }//while return 0;}
[Huawei on-machine exercises] 6. Sorting integers