来自《C/C++软件工程师就业求职手册》 p150
书中的解法是错的
#include <iostream>
using namespace std;
void Calculate(const char* str,int& max0,int& max1)
{
int temp0 = 0; //保存连续0的长度
int temp1 = 0;
while(*str) //遍历字串
{
if(*str=='0') //当前的是0
{
max0++;
if(*(++str)=='1') //如果下一个字符是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;
}