找出01字符串中0和1连续出现的最大次数
来源:互联网 发布:高压电棍专卖淘宝 编辑:程序博客网 时间:2024/04/30 04:43
//找出01字符串中0和1连续出现的最大次数#include <stdio.h>void Count(char *str, int *max0, int *max1){ int temp0 = 0; int temp1 = 0; while (*str++) { if (*str == '0') { temp0++; if (*(str+1) == '1') { if (temp0 >*max0) *max0 = temp0; temp0=0; // remember reset 0 } } else if (*str == '1') { temp1++; if (*(str+1) == '0') { if (temp1>*max1) *max1 = temp1; temp1=0; // remember reset 0 } } }}int main(){ char *s = "0010101111100011000000110011"; int max0 = 0; int max1 = 0; Count(s, &max0, &max1); printf("max0:%d\nmax1:%d\n", max0, max1); return 0;}