Simple string processing nyoj525

Source: Internet
Author: User
Tags strtok

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
This question can be solved by sscanf. I have reviewed the sscanf usage and learned about strtok usage,
AC code
# Include <iostream> # include <stdio. h> # include <cstring> # include <string> # include <algorithm> using namespace STD; char STR [5005]; int A [5005]; int main () {While (scanf ("% s", STR )! = EOF) {Int J, I = 0, Len = strlen (STR), k = 0, num; for (I = 0; I <Len; ++ I) if (STR [I] = '5') STR [I] = ''; I = 0; For (j = 0; j <Len; ++ J) if (STR [J]! = '') {Sscanf (STR + J," % I % N ", & A [k ++], & num ); // % indicates selecting I as the selection condition, integer, and N as the selected number. Note that the conversion characters include \ 0. j = J + num;} Sort (A, A + k); If (k = 0) printf ("0 \ n"); For (INT q = 0; q <K; ++ q) printf ("% d", a [Q]); printf ("\ n") ;}// system ("pause "); return 0 ;}

Strtok usage, from Baidu Baike ~

Sample Code:

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main ()
{
Char sentence [] = "this is a sentence! ";
Cout <sentence <Endl;
Char * PTR = strtok (sentence ,"");
While (PTR! = NULL)
{
Cout <PTR <Endl;
PTR = strtok (null ,"");
}
System ("pause ");
Return 0;
}

Two parameters must be set for the first function call. The result of the first split returns the first ',' string in the string, that is, the first output of this by the above program.

The second call to this function strtok (null, ","). The first parameter is set to null. The result is returned Based on the subsequent string, that is, the second output is.

 

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.