Find Maximums
constrains : The first line of the input contains two integers n — the size of the array (1 ≤ n ≤ 1000) and k (1 ≤ k ≤ n).
The second line of the input contains n integers a1, a2, ..., an, (1 ≤ ai ≤ 1000) where ai is equal to the element at the i-th positio
input
6 3 8 4 8 2 3 7output
8 8 8 7
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM(a,b) ( (a>b) ? a : b )
int main(void) {
int ARR_SIZE;
int k;
int *p;
int i , j;
int last;
int max;
int temp;
max=0;
scanf("%d %d",&ARR_SIZE,&k );
if( (ARR_SIZE < 1) || (ARR_SIZE > 1000) || (k < 1) || (k > ARR_SIZE) )
return 0;
p=(int*)malloc(sizeof(int) * ARR_SIZE);
for(i=0;i<ARR_SIZE;i++)
{
scanf("%d",&p[i]);
if(p[i] > 1000 || p[i] < 1 )
{
return 0;
}
}
last=ARR_SIZE-k;
for(i=0;i<=last;i++)
{
for(j=k+i-1; j > i ; j--)
{
temp=MAXIMUM(p[j] , p[j-1]);
max=MAXIMUM(max,temp);
}
printf("%d ",max);
max=0;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire