数组移动窗口等步长输出最大最小值

例如:一个数组 a[15] = {1,6,3,7,2,9,4,1,0,3,7,2,11,53,12}以等步长3,依次输出该步长内的最小值。

题型1:等步长为3,将数组按照5段均分,即输出5个最小值。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//#include <cstddef>
static int step = 0;
int main(int argc, char* argv[])
{
    int a[15] = {1,6,3,7,2,9,4,1,0,3,7,2,11,53,12};
    int length = 3;
    int time;
    for(time = 0; time < 15/3; time++)
    {
    
        int minNum = 9999999;
        int i;
        for(i= step; i< step + length; i++)
        {   
            if(minNum >= a[i])
               minNum = a[i];
        }
        step += 3;
        printf("%d\n", minNum);
    }
    
    return 0;
}  //输出结果:1 2 0 2 11

 

 

题型2:等步长为3,窗口一步一步移动,即先输出0-3个元素中最小值,再输出1-4个元素中的最小值,接着输出2-5个元素中的最小值,以此类推。。。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//#include <cstddef>
static int step = 0;
int main(int argc, char* argv[])
{
    int a[15] = {1,6,3,7,2,9,4,1,0,3,7,2,11,53,12};
    int length = 3;
    int time;
    for(time = 0; time <= 15-3; time++)   //此处修改
    {
    
        int minNum = 9999999;
        int i;
        for(i= step; i< step + length; i++)
        {
            
            if(minNum >= a[i])
               minNum = a[i];
        }
        step += 1;    //此处修改
        printf("%d\n", minNum);
    }
    return 0;
}

 

posted @ 2020-10-23 10:08  楚天耀景  阅读(162)  评论(0)    收藏  举报