标签:
//the first k elements interviewed and rejected,
//for the latter n-k elements ,if value >max,return value, else return n‘elements.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
#define RANDOM_SIZE 100
int buf [ARRAY_SIZE];
int main()
{
srand((unsigned int )time(0));
int i,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%RANDOM_SIZE;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("*\n");
//creat random value of buffer and print
//online maximum
int k=n/2,max=-RANDOM_SIZE;
for(i=1;i<=k;i++)
{
if(max<buf[i])max=buf[i];
}
for(i=k+1;i<=n;i++)
{
if(buf[i]>=max)
{
printf("%d\n",buf[i]);
exit(0);
}
}
printf("%d\n",buf[n]);
}
}
CLRS: online maximum (n,k)algorithm
标签:
原文地址:http://www.cnblogs.com/zeroArn/p/5462426.html