// plateau.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void plateau(int arry[],int n)
{
int count,max_num,max_count;
count=1;
max_count=1;
max_num=arry[0];
for(int i=1;i<n;i++)
{
if(arry[i]==arry[i-1])
count++;
else
{
if(count>max_count)
{
max_count=count;
max_num=arry[i-1];
if(max_count>=(n-i))
{
break;
}
count=1;
}
else
{
count=1;
}
}
}//for
if(i==n && count>max_count)
{
max_count=count;
max_num=arry[n-1];
}
printf("max_count=%d,max_num=%d\n",max_count,max_num);
}
int longest_plateau(int x[], int n)
{
int length = 1; /* plateau length >= 1. */
int i;
for (i = 1; i < n; i++)
if (x[i] == x[i-length])
length++;
return length;
}
int main(int argc, char* argv[])
{
int a[]={1,2,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,7,7,8};
plateau(a,sizeof(a)/sizeof(int));
printf("Hello World!\n");
printf("\n\nLength of the Longest Plateau is %d\n",
longest_plateau(a,sizeof(a)/sizeof(int)));
return 0;
}
/*
max_count=6,max_num=5
Hello World!
Length of the Longest Plateau is 6
Press any key to continue
*/
plateau最长平台
最新推荐文章于 2024-12-04 18:27:10 发布