c语言判断连续是0 或1 的最大次数

来源:互联网 发布:php替换字符串中的字符 编辑:程序博客网 时间:2024/05/05 11:16



python贴吧上有人问,从终端读入一个整数n,随机一个输入一个0 或1


判断连续是0 或1 的最大次数。如:


输入


0


0


0


1


1


1


1


0


1


0


1在连续输入中,出现4次 ,将python代码修改为c语言代码。
python代码请看http://my.oschina.net/pythonpeixun/blog/380293




/******************************************************
python北京周末培训班
https://github.com/pythonpeixun/article/blob/master/beijing_weekend.md
python上海周末培训班
https://github.com/pythonpeixun/article/blob/master/shanghai_weekend.md
python远程视频培训
https://github.com/pythonpeixun/article/blob/master/index.md
c语言从入门到精通远程视频培训
https://github.com/pythonpeixun/article/blob/master/c_course.md
咨询:qq:1465376564  迪艾姆python培训黄哥
******************************************************/
 
#include <stdio.h>
#include <string.h>
 
void append(int arr[], int number);
int max_array(int a[], int num_elements);
 
int main()
{
    int number[100], total_num[100] = {0};
    int n, i, j;
    printf("please input N:\n");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("please input 数字0或1:\n");
        scanf("%d", &number[i]);
    }
    j = 1;
    for (i = 0; i < n - 1; i++){
        if (number[i] == number[i+1])
            j += 1;
        else
        {
            append(total_num, j);
            j = 1;
        }
    }
    append(total_num, j);
    printf("最大连续数字出现的次数是: %d\n", max_array(total_num, n));
 
}
 
void append(int arr[], int number)
{   
    int i = 0;
    while (1)
    {
        if (arr[i] == 0)
        {
            arr[i] = number;
            break; 
        }
        i++;    
    }
     
}
 
int max_array(int a[], int num_elements)
{
   int i, max = 0;
   for (i = 0; i < num_elements; i++)
   {
     if (a[i]>max)
     {
        max=a[i];
     }
   }
   return max;
}
0 0