8 solutions

  • 0
    @ 2022-2-16 21:47:47

    每次取最小的两个数相乘+1最终结果会得到最大值,相反会得到最小值,因此,建立两个数组利用排序分别按从小到大以及从大到小的顺序排列

    #include<stdio.h>
    #include<stdlib.h>
    #define ll long long
    //每次取最小的两个数最终可以得到max
    //每次取最大的两个数最终可以得到min 
    int array[50002];
    int attr[50002];
    int comp(const void *a,const void *b){
    	return *(int *)a-*(int *)b;
    }
    int comp2(const void *a,const void *b){
    	return *(int *)b-*(int *)a;
    }
    int main(){
    	int n;
        int sum;
    	int max;
    	int min;
    	int res;
    	int length;
    	int length2;
    	int len;
    	scanf("%d",&n);
    	for(int i=0; i<n+1; i++){
    		scanf("%d",&array[i]);
    		attr[i] = array[i];
    	}
    	length = n;
    	length2 = n;
    	qsort(array,n,sizeof(int),comp);//调用排序函数
        for(int i=0; i<n-1; i++){//2 4 5 6
        	max = array[i]*array[i+1]+1;
        	array[i+1] = max;
        	qsort(&array[i+1],--length,sizeof(int),comp);
    	}
    	qsort(attr,length2,sizeof(int),comp2);
    	for(int i=0; i<n-1; i++){//2 4 5 6
        	min = attr[i]*attr[i+1]+1;
        	attr[i+1] = min;
        	qsort(&attr[i+1],--length2,sizeof(int),comp2);
    	}
        printf("%d",max-min);
    }
    

    Information

    ID
    95
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    130
    Accepted
    48
    Uploaded By