Nyoj 525 a water question

Source: Internet
Author: User

A simple question is a "water question". You only need to consider various situations. In special cases, the values are 5, and the values are 0, 1235555, and 555123. In my opinion, this is the case. You only need to consider it as complete. Question:

A water Question Time Limit: 1000 MS | memory limit: 65535 kb difficulty: 2
Description
Today, lzq is playing a small game, but the number of this game is a little big. He is a little tired and wants to pull some people in to help him. Can you write a program to help him? This game is like this: there is a line of numbers. If we regard '5' in this line of numbers as spaces, then several non-negative integers (some integers may start with '0' and the '0' of these headers should be ignored, unless this integer is composed of several '0', then this integer is 0 ).
Your task is to sort and output the generated integers in ascending order. Please write a program to help lzq!
Input
The input contains multiple groups of test cases. Each group of input data has only one row of numbers (no space between numbers), and the length of this line of numbers is not greater than 5000.

Input data guarantee: The calculated non-negative integer is not greater than 100000000. If all input data is 5, 0 is output.

Output
For each test case, the result of sorting the separated integers is output. Two Adjacent integers are separated by a space. Each group of output occupies one row.

Sample Input
0051231232050775
Sample output
0 77 12312320

AC code:

#include <iostream>#include <string>#include <cstdio>#include <algorithm>using namespace std;int num[5010];int fun(int x,int y){if(y==0)return x;else{int s=1;for(int j=1;j<=y;++j){  s*=10;}return x*s;}}int main(){//freopen("1.txt","r",stdin);string ss;while(cin>>ss){int k=0,sum=0,front=0,behind=0;int len=ss.length();for(int i=0;i<len;++i){if(i==len-1&&ss[i]!='5'){  sum=0;  behind=i;  for(int j=behind,s=0;j>=front;--j){    sum+=fun(int(ss[j]-'0'),s++);  }  num[k++]=sum;}if(ss[i]=='5'){  sum=0;  behind=i-1;  for(int j=behind,s=0;j>=front;--j){    sum+=fun(int(ss[j]-'0'),s++);  }  int x=front;  front=i+1;  if(behind-x==-1){ continue;  }  num[k++]=sum;}}if(k==0)puts("0");else{sort(num,num+k);for(int i=0;i<k;++i)printf("%d ",num[i]);puts("");}}return 0;}

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.