字符串------找出01串中o和1连续出现的最大次数

题目:

思路:

  1. 记录遍历中的最大值,与temp比较,并交换

代码:

void Calculate(const char *str,int *max0,int *max1)
{
	int temp0=0;        //保存0串的最大长度
	int temp1=0;       //保存1串的最大长度
	
	while(*str)
	{
		if(*str=='0')
		{
			(*max0)++;             //0的长度
			if(*str++ == '1')        //如果下一个是1
			{
				if(temp0<*max0)        //判断当前长度是否需要保存
				{
					temp0=*max0;
				}
				*max0=0;
			}
		}
		else if(*str=='1')
		{
			(*max1)++;             //1的长度
			if(*str++ == '0')        //如果下一个是1
			{
				if(temp1<*max1)        //判断当前长度是否需要保存
				{
					temp1=*max1;
				}
				*max1=0;
			}
		}
	}
	*max0=temp0;
	*max1=temp1;
}

猜你喜欢

转载自blog.csdn.net/qq_39503189/article/details/82707387