零幺串

用1和0组成的串为“零幺串”。编写程序实现,任意输入零幺串S,将返回一个值N1和一个值N0,其中N1表示S中最长幺串的长度,N0表示S中最长零串的长度。例如假设S是
00010111001110001111,则返回的N0 = 3,N1 = 4.
代码:

#include <stdio.h>
#include <string.h>
void countNum(char* str,int* N0,int* N1);
int main()
{
	char str[30];
	gets(str);  //gets()其作用是从终端输入一个字符串到字符数组 
	int iNum0=-1;
	int iNum1=-1;
	countNum(str,&iNum0,&iNum1);
	printf("%s: NO=%d,N1=%d.\n",str,iNum0,iNum1);
	return 0;
}
void countNum(char* str,int* N0,int* N1)   //指针变量作为函数参数 
{
	int maxNum0=0;
	int maxNum1=0;
	int length = strlen(str);
	int count=0;
	int i=0;
	for(i=0;i<length;i++)                 //计数0的最长串 
	{
		if(str[i]=='0') {
			count ++;
			if(count > maxNum0) {
 				maxNum0=count;
			}
		}else {                     //每遇到1,count就清零 
			count=0;
		}
	}
	count=0;                        //count清零,准备计数1的最长子串 
	for(i=0;i<length;i++) {
		if(str[i]=='1') {
 			count ++;
			if(count > maxNum1) {
				maxNum1 = count;
			}
		}else {                 //每遇到0,count就清零 
			count=0;
   		}
	}
	(*N0) = maxNum0;
	(*N1) = maxNum1;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值