统计一个01字符串中0,1连续出现的最大次数
2015-08-31 16:22
519 查看
比如:0011000111
则表示0最大出现3次,1最大出现3次。
程序的思路很巧妙,不复杂。
测试0101000000111111101003
则表示0最大出现3次,1最大出现3次。
程序的思路很巧妙,不复杂。
// demo.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; static void strwe(char *str) { int len=0; while(*str++) len++; //while循环结束以后 得到的就是字符串的实际长度 str-=2; //while循环结束以后 str实际指向str字符串的结束符飞‘\0’的下一个字符 } static void delete_char(char *str,char c) //删除字符串中指定的字符 { if (str==NULL) { return ; } char *p=str; while(*p++) { if (*p!=c) { *str++=*p; } } *str='\0'; } static void caculate01(const char *str,int &max0,int &max1) { int temp0=0; int temp1=0; if (!str) { return ; } while(*str) { if (*str=='0') { max0+=1; if (*(str+1)=='1') { if (max0>temp0) { temp0=max0; } max0=0; } } else if (*str=='1') { max1+=1; if (*(str+1)=='0') { if (max1>temp1) { temp1=max1; } max1=0; } } str++; } max0=(max0>temp0)?max0:temp0; max1=(max1>temp1)?max1:temp1; //这里这样写的目的是防止01010001111样式的字符串 所带来的bug //如果你不按照上两行这样写 你测试01010001111会得到 3 1 而不是 3 4 } int main() { char str[]="01010001111"; int max0=0; int max1=0; caculate01(str,max0,max1); cout<<"0连续出现的次数最多为:"<<max0<<endl; cout<<"1连续出现的次数最多为:"<<max1<<endl; return 0; }
测试0101000000111111101003
相关文章推荐
- 【Matlab学习笔记】(六)基本数据结构(矩阵定义方法)
- C++类的空间大小
- C语言中设置进程优先顺序的方法
- C# 获取当前路径方法(经典&重要)
- 堆排序 感觉忘了实现一遍
- 天线笔记
- .net学习笔记---xml操作及读写
- Java并发编程之闭锁CountDownLatch和栅栏(CyclicBarrier)
- UITableView-------模型的使用
- Linux下,多线程程序死循环问题调试
- poj 2367 Genealogical tree
- .bash_profile 配置
- 设计模式之建造者模式
- C#中几种int转换形式
- 基于H5+ API手机相册图片压缩上传
- 一个非常高效的去重且保持原顺序算法
- 运行javascript的方式
- .net学习笔记---xml基础知识
- [转] Hibernate与 MyBatis的比较
- Xshll 登陆linux很慢解决方式