6-5 数组区段的最大最小值

文章描述了一个C语言函数`sublistMaxMin`,用于查找给定数组从`from`到`to`地址范围内元素的最大值和最小值。函数通过遍历指定区间内的元素来确定这两个值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本题要求实现一个函数,找出数组中一部分数据的最大值和最小值。
题目保证没有无效数据。 

函数接口定义:

void sublistMaxMin ( int* from, int* to, int* max, int* min );

其中 from和to都是用户传入的参数,分别存放数组部分数据的起始地址和结束地址,并且from<=to。
其中max和min为用户传入的地址,分别用于在sublistMaxMin中保存from至to对应区段中数组元素的最大值和最小值的地址。

裁判测试程序样例:

#include <stdio.h>
void sublistMaxMin ( int* from, int* to, int* max, int* min );
int main()
{
    int list[1000];
    int len=0;
    int from, to, max, min;
    scanf("%d", &len);
    int i;
    for(i=0; i<len; i++){
        scanf("%d", &list[i]);
    }
    scanf("%d%d", &from, &to);
    sublistMaxMin(list+from, list+to, &max, &min);
    printf("list[%d-%d]: max = %d, min = %d\n", from, to, max, min);
    return 0;
}



/* 请在这里填写答案 */

 

输入样例:

5
1 2 3 4 5
0 4

输出样例:

list[0-4]: max = 5, min = 1

void sublistMaxMin ( int* from, int* to, int* max, int* min )
{
    int length=to-from;
    *max=*from;
    *min=*from;
    
    for(int i=0;i<=length;i++)
    {
        if(*(from+i)>*max)
        {
            *max=*(from+i);
        }
        
        if(*(from+i)<*min)
        {
            *min=*(from+i);
        }
    }

}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值