标签:算法提高 最大乘积
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int a[20]; int main() { int n,i,ans,m,k; scanf("%d",&k); while(k--){ scanf("%d%d",&n,&m); for(i=0;i<n;++i){ scanf("%d",&a[i]); } sort(a,a+n);ans=1; int value=1,result=1,e=0,g=n-1,t=0; if(m%2==0){ while(t!=m){ value=a[e]*a[e+1];result=a[g]*a[g-1]; if(value>result){ ans*=value;e+=2; } else { ans*=result;g-=2; } t+=2; } printf("%d\n",ans); } else { m=m-1;g=g-1; while(t!=m){ value=a[e]*a[e+1];result=a[g]*a[g-1]; if(value>result){ ans*=value;e+=2; } else { ans*=result;g-=2; } t+=2; } printf("%d\n",ans*a[n-1]); } } return 0; }
标签:算法提高 最大乘积
原文地址:http://blog.csdn.net/r1986799047/article/details/43852521