[英]How to find maximum(or minimum) in any subarray(of any size) in a given array?
[英]Is there any predefined function in C to find the minimum or maximum element from a given array?
我有一个数组: int a[6] = {3, 4, 1, 2, 5, 6}
我想通过使用预定义的 C function 找到最小和最大元素; 有吗?
您可以使用qsort
内置函数对数组进行排序,然后第一个元素最小,最后一个元素最大。
int size=6;
int compare( const void* a, const void* b)
{
int int_a = * ( (int*) a );
int int_b = * ( (int*) b );
// an easy expression for comparing
return (int_a > int_b) - (int_a < int_b);
}
qsort(values, size, sizeof(int), compare )
printf("smallest:%d",values[0]);
printf("largest:%d",values[size-1]);
不,标准库中没有minmax
函数,但您可以自己创建一个。
例子:
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
// a struct to hold the min and max result
typedef struct {
int min;
int max;
} mm;
// a function to find the min and max values
mm minmax(const int *arr, size_t len) {
mm res = {INT_MAX, INT_MIN}; // start with min = INT_MAX, max = INT_MIN
for(const int *end = arr + len; arr != end; ++arr) {
if(*arr < res.min) res.min = *arr;
if(*arr > res.max) res.max = *arr;
}
return res;
}
int main() {
int a[6] = {3, 4, 1, 2, 5, 6};
mm res = minmax(a, sizeof a / sizeof *a);
printf("min: %d max: %d\n", res.min, res.max);
}
这可以推广为一个函数,它可以在任何类型的数组中找到最小和最大元素,就像qsort
函数可以对任何类型元素的数组进行排序,这些元素以严格的弱排序方式进行比较。
#include <stddef.h>
// a struct to hold pointers to the min and max elements
typedef struct {
const void *min;
const void *max;
} mm;
// The signature of a function for comparing elements.
// Such a function should return
// -1 if the left hand side is less than the right
// +1 if the right hand side is greater than the left
// 0 otherwise
typedef int (*comp_func)(const void *, const void *);
// the minmax function now takes these arguments:
// in_arr : a "const void*" to the array
// count : the number of elements
// size : the size of an element
// compare: a pointer to a function capable of comparing two elements
mm minmax(const void *in_arr, size_t count, size_t size, comp_func compare) {
mm res = {0}; // both the min and max pointers a NULL
if(count) {
// "cur" and "end" are here pointers to "const char[size]" elements,
// so "++cur" will step "size" bytes in memory:
const char (*cur)[size] = in_arr;
const char (*end)[size] = cur + count;
res.min = cur; // use the pointer to the first value as the pointer to min...
res.max = cur; // ...and max
for(++cur; cur != end; ++cur) {
// call the compare function
if(compare(cur, res.max) == 1) res.max = cur;
else if(compare(cur, res.min) == -1) res.min = cur;
}
}
return res;
}
有了那个minmax
函数,您就可以将它用于int
或任何类型的数组。 您只需要提供一个函数来对两个元素进行实际比较。
例子:
#include <stdio.h>
int int_compare(const void *lhs, const void *rhs) {
return *((int*)lhs) < *((int*)rhs) ? -1 :
*((int*)lhs) > *((int*)rhs) ? 1 : 0;
}
int main() {
int a[6] = {3, 4, 1, 2, 5, 6};
mm res = minmax(a, sizeof a / sizeof *a, sizeof *a, int_compare);
// dereference the min and max pointers to get the values:
printf("min: %d max: %d\n", *((int*)res.min), *(int*)res.max);
}
不,没有标准的 C 函数。 但是你可以自己做
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
是大是小,从if里面的对比就一目了然了。 如果 >,很大。 如果 <, 很小
正如 Ted Lyngmo 所说,没有,但您可以创建自己的。 但是请注意,如果您希望同时计算最小值和最大值,则可以在最坏的情况下将比较总数减少到 3n/2,使用以下技巧使用 3 个比较处理 2 个元素:
typedef struct {
int min;
int max;
} MinMax;
MinMax minmax(const int *arr, size_t len) {
MinMax res = {INT_MAX, INT_MIN};
while (len >= 2) {
int a = arr[0];
int b = arr[1];
if (a < b) {
if (a < res.min) res.min = a;
if (b > res.max) res.max = b;
} else {
if (b < res.min) res.min = b;
if (a > res.max) res.max = a;
}
arr += 2;
len -= 2;
}
if (len == 1) {
if (*arr < res.min) res.min = *arr;
if (*arr > res.max) res.max = *arr;
}
return res;
}
C99 包含math.h
这有函数 - fmin、fmax、fminl、fmaxl、fmaxf、fminf
#include <math.h>
double fmin( double x, double y );
float fminf( float x, float y );
long double fminl( long double x , long double y );
fmin() 函数返回较小参数的值。
#include <math.h>
double fmax( double x, double y );
float fmaxf( float x, float y );
long double fmaxl( long double x , long double y );
fmax() 函数返回较大参数的值。
所以,对于答案——
float max=0;
for (i = 0; i < n; i++)
max = fmaxf (max, arr[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.