[Huawei Machine Test exercises]43. Find the longest consecutive number string in a string

Source: Internet
Author: User
Tags string back

Topic

Describe:

请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!

Sample input

abcd12345ed125ss123058789abcd12345ss54761

Sample output

输出123058789,函数返回值9输出54761,函数返回值5

Function Prototypes:

   unsignedint Continumax(char** pOutputstr,  char* intputstr)

Input parameters:

   char* intputstr  输入字符串;

Output parameters:

   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;  

return value:

  连续最长的数字串的长度

Practice Stage:

初级  

Code One

/* ---------------------------------------* Date: 2015-07-03* sjf0115* title: Find the longest consecutive number string in a string * Source: Huawei Machine Test Exercises------------- ----------------------------*/#include <iostream>#include <stdlib.h>#include <string>#include "oj.h"using namespace STD;/ * Function: Finds the longest consecutive number string in the string and returns the length of the string back to the function prototype: unsigned int continumax (char** poutputstr, char* intputstr) input parameters: char* INTP   UTSTR input String output parameter: char** POUTPUTSTR: The longest consecutive number string, if the length of the longest consecutive number string is 0, should return an empty string poutputstr points to the memory should be within the function with the malloc function request, the call is responsible for releasing the return value: Length of the longest consecutive number string * /unsigned intContinumax (Char* * Poutputstr,Char* Intputstr) {if(Intputstr = = NULL) {return 0; }//if    intSize =strlen(INTPUTSTR); *poutputstr =New Char[size+1];intStart =0, end =0, Maxstart =0, Maxend =0;intMax =0; for(inti =0; i < size;++i) {//Digital        intCount =0; start = i;//Statistics Continuous digital         while(I < size && Intputstr[i] >=' 0 '&& Intputstr[i] <=' 9 ') {++i;        ++count; }//if        //Update maximum value        if(Max <= count)            {max = count;            Maxstart = start;        Maxend = i; }//if}//for    //Output    intindex =0; for(inti = Maxstart;i < Maxend;++i) {(*POUTPUTSTR) [index++] = intputstr[i]; }//for(*POUTPUTSTR) [Index] =' + ';returnMaxend-maxstart;}

Code two

/* ---------------------------------------* Date: 2015-07-03* sjf0115* title: Find the longest consecutive number string in a string * Source: Huawei Machine Test Exercises------------- ----------------------------*/#include <iostream>#include <stdlib.h>#include <string>#include "oj.h"using namespace STD;/ * Function: Finds the longest consecutive number string in the string and returns the length of the string back to the function prototype: unsigned int continumax (char** poutputstr, char* intputstr) input parameters: char* INTP   UTSTR input String output parameter: char** POUTPUTSTR: The longest consecutive number string, if the length of the longest consecutive number string is 0, should return an empty string poutputstr points to the memory should be within the function with the malloc function request, the call is responsible for releasing the return value: Length of the longest consecutive number string * /unsigned intContinumax (Char* * Poutputstr,Char* Intputstr) {if(Intputstr = = NULL) {return 0; }//if    intSize =strlen(INTPUTSTR); *poutputstr =New Char[size+1];intStart =0, end =0, Maxstart =0, Maxend =0;intMax =0;BOOLIsnum =false; for(inti =0; I <= size;++i) {//Digital        if(Intputstr[i] >=' 0 '&& Intputstr[i] <=' 9 ') {++end; Isnum =true; }//if        //Non-digital        Else{if(Isnum | | i = = size) {if(Max <= (End-start))                    {max = End-start;                    Maxstart = start;                Maxend = end; }//if}//ifStart = i +1; end = i +1; Isnum =false; }//else}//for    //Output    intindex =0; for(inti = Maxstart;i < Maxend;++i) {(*POUTPUTSTR) [index++] = intputstr[i]; }//for(*POUTPUTSTR) [Index] =' + ';returnMaxend-maxstart;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine Test exercises]43. Find the longest consecutive number string in a string

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.