[Huawei on-machine exercises] 6. Sorting integers

Source: Internet
Author: User

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&GT;&GT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.