current position:Home>Find the maximum difference between any two data in the array

Find the maximum difference between any two data in the array

2022-01-27 01:08:02 CSDN Q & A

The phenomenon and background of the problem

img

#include <stdio.h>#include <stdlib.h>#include <math.h>int seek_Max(int* p, int n) {    int i, min;    min = *p;    int ret = *(p+ 1) - *p;    for (i = 1; i < n; i++) {        if (*(p + i) - min > ret) {            ret = *(p + i) - min;        }        if (*(p + i) < min) {            min = *(p + i);        }    }    return ret;}int main() {    int n, i, rt;    scanf("%d", &n);    int* p = (int*)malloc(sizeof(int) * n);    if (!p) {        return 0;    }    for (i = 0; i < n; i++) {        scanf("%d", p + i);    }    rt = seek_Max(p, n);    printf("%d", rt);    free(p);    p = NULL;    return 0;}
Operation results and error reporting contents

I've tested myself many times , The answers meet the requirements , But it can't pass the test of the question brushing website , Always feed back the wrong answer




Refer to the answer 1:

Just find out the difference between the maximum and minimum values

int seek_Max(int* p, int n) {    int i, min,max;    min = max = p[0];    for (i = 1; i < n; i++) {        if(p[i] > max)            max = p[i];        if(p[i] < min)            min  = p[i];    }    return max - min;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270108002887.html

Random recommended