65,186
社区成员




#include <iostream>
void calculate0and1();
using namespace std;
void calculate0and1( char *str,int *max0,int *max1)
{
int temp0 = 0;
int temp1 = 0;
while (*str)
{
if (*str ==' 0')
{
(*max0)++;
if (*(++str) == '1')
{
if (temp0 < *max0)
{
temp0 = *max0;
}
*max0 = 0;
}
}
else if (*str == '1')
{
(*max1)++;
if (*(++str) == '0')
{
if (temp1 < *max1)
{
temp1 = *max1;
}
*max1 = 0;
}
}
}
*max0 = temp0;
*max1 = temp1;
}
int main()
{
char string[] = "00001111001010101010101010001011101001010";
int max0 = 0;
int max1 = 0;
calculate0and1(string, &max0, &max1);
cout << max0 << max1 << endl;
return 0;
}
#include <stdio.h>
void calculate0and1( char *str,int *max0,int *max1)
{
int temp0 = 0;
int temp1 = 0;
while (*str)
{
temp0 = 0;
temp1 = 0;
while(*str && (*str=='1'))
{
temp1++;
str++;
}
*max1 = temp1>*max1?temp1:*max1;
while(*str && (*str=='0'))
{
temp0++;
str++;
}
*max0 =temp0>*max0?temp0:*max0;
}
}
int main()
{
char string[] = "00001111001010101010101010001011101001010";
int max0 = 0;
int max1 = 0;
calculate0and1(string, &max0, &max1);
printf("%d\n",max0);
printf("%d\n",max1);
return 0;
}
希望楼主学语言的时候学到精髓,下意识的运用一门语言的特点去解决问题.void calc0And1(string str,int& max0,int& max1 )
{
int count = 0;
int tmp0 = max0;
int tmp1 = max1;
string::iterator pos = str.begin();
for (string::iterator it = str.begin();it != str.end();)
{
if (*it == '1')
{
while (it != str.end() && *it == '1')
{
it++;
count++;
}
tmp1 = it-pos;
pos = it;
}
else
{
while(it != str.end() && *it == '0')
{
it++;
count++;
}
tmp0 = it-pos;
pos = it;
}
max0 = max0<tmp0?tmp0:max0;
max1 = max1<tmp1?tmp1:max1;
}
}
int main()
{
string str("00001111001010101010101010001011101001010");
int max0 = 0;
int max1 = 0;
calc0And1(str, max0, max1);
cout << max0 <<endl << max1 << endl;
return 0;
}
while (*str)
{
if (*str ==' 0')
{
(*max0)++;
if (*(++str) == '1')
{
if (temp0 < *max0)
{
temp0 = *max0;
}
*max0 = 0;
}
}
else if (*str == '1')
{
(*max1)++;
if (*(++str) == '0')
{
if (temp1 < *max1)
{
temp1 = *max1;
}
*max1 = 0;
}
}
}