1225 Problem R- segmentation sort - Getting title - string handling -C ++ to achieve

Problems R: dividing sorting

Time limit: 1 Sec memory limit: 32 MB
submitted: 211 solution: 61

Title Description

Enter a column of figures, if we put line numbers in the '5' are seen as a space, then get a number of non-negative integers separated by a space line (there may be some integer with leading '0', the head of the '0' should It is ignored, unless this is the integer number '0' composition, then the integer is 0).
Your task is: integer obtained by dividing these, according to the order from small to large sorting output.

Entry

Test input comprising a plurality of sets of data. Each set of digital input data line only (no spaces between the digits), digital line length is no greater than 1000.  
Input data to ensure that: the non-negative integer not greater than 100 million obtained by dividing the input data can not all the '5' composition.

Export

The results for each test case sorted integer, obtained by dividing the output, between two adjacent integers separated by a space, each output per line.

Sample input  Copy

0051231232050775

Sample output  Copy

0 77 12312320

Code

prompt:

Error conditions
 
analysis
50 errors Practice, I do not know where wrong but most have passed the test sample
100 correct Fucking afternoon, because the result is an array of a capacity too small.
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main(){
    string s;
    while(cin>>s){
        long long int a[500];
        int j=0,num=0,k=0;
        int len=s.size();//输入字符串长度
        s[len]='5';
        if(s[0]=='5'){ //字符串开头为5,下一个字符开始遍历
            k=1;
        }
        for( int i=k;i < len+1;i++){//core part
            if(((i>=1)&&(s[i]=='5')&&(s[i-1]=='5'))){
                continue;//前后字符都为5,跳过,继续遍历
            }
            if(s[i]!='5'){//记录数字
                num*=10;
                num+=s[i]-'0';
            }
            else{ //遇到5,存储前面的数字
                    a[j]=num;
                    j++;
                    num=0;
            }
        }
        sort(a,a+j);//排序
        for(int i = 0;i < j;i++){ //输出
            cout<<a[i];

            if(i<j-1){ //非结尾输出空格
                cout<<' ';
            }
            else{ //结尾输出换行符
                cout<<endl;
            }       
        }
    }
    return 0;
}


 

Published 20 original articles · won praise 0 · Views 118

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/104736460