#include<stdio.h> #include<math.h> #define max 100005 long long n=0; long long MAX(long long *p); long long MIN(long long *q); long long number[max],number1[max],number2[max],number3[max],number4[max]; int main() { long long j,i=0,min=0,count,temp; long long result[4]; scanf("%lld",&n); while(i<n) { scanf("%lld",&number[i]); number1[i]=(i+1)*(i+1)+number[i]*number[i]; number2[i]=number[i]*number[i]-(i+1)*(i+1); number3[i]=(i+1)*(i+1)-number[i]*number[i]; number4[i]=-(i+1)*(i+1)-number[i]*number[i]; i++; } result[0]=MAX(number1)-MIN(number1); result[1]=MAX(number2)-MIN(number2); result[2]=MAX(number3)-MIN(number3); result[3]=MAX(number4)-MIN(number4); for(i=0;i<3;i++) { for(j=0;j<3-i;j++) { if(result[j]<result[j+1]) { temp=result[j]; result[j]=result[j+1]; result[j+1]=temp; } } } printf("%lld",result[0]); } long long MAX(long long *p) { long long i=0,ma=0; ma=p[0]; for(i=1;i<n;i++) { if(p[i]>ma) ma=p[i]; } return ma; } long long MIN(long long *p) { long long i=0,min=0; min=p[0]; for(i=1;i<n;i++) { if(p[i]<min) min=p[i]; } return min; }
几个关键点:数字要longlong,否则会爆内存
时间问题:如果先用排序来做要注意时间复杂度,不能用冒泡排序。
用直接查找最大值和最小值的方法比较好