码迷,mamicode.com
首页 > 其他好文 > 详细

CLRS: online maximum (n,k)algorithm

时间:2016-05-05 17:38:58      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

//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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!