The maximum length of a string 0 or 1 in a row exe6 (the result is incorrect. It is related to the input string)

Source: Internet
Author: User

From job application manual for C/C ++ software engineers p150

The solution in the book is wrong.

 

# Include <iostream>
Using namespace STD;

 

Void calculate (const char * STR, Int & max0, Int & max1)
{
Int temp0 = 0; // Save the length of 0 consecutive times
Int temp1 = 0;
While (* Str) // traverses the string
{
If (* STR = '0') // The current value is 0.
{
Max0 ++;
If (* (+ + Str) = '1') // if the next character is 1
{
If (max0> temp0)
Temp0 = max0;
Max0 = 0;
}
}
Else if (* STR = '1 ')
{
Max1 ++;
If (* (+ + Str) = '0 ')
{
If (max1> temp1)
Temp1 = max1;
Max1 = 0;
}
}
}
Max0 = temp0;
Max1 = temp1;
}

 

Int main ()
{
Char * string = "000110000000000 ";
Int max0 = 0;
Int max1 = 0;
Calculate (string, max0, max1 );
Cout <"max0 =" <max0 <Endl;
Cout <"max1 =" <max1 <Endl;
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.