正负分开
都按照负数的话可以以绝对值从大到小排序,省点事
#include<iostream>#include<algorithm>#include<vector>#include<map> #include<string>#include<set>using namespace std;vector<int> pcoup,ncoup,pPRod,nprod;int main(){ int nc,np; cin>>nc; for(int i = 0; i < nc; i++){ int temp; scanf("%d",&temp); if(temp > 0){ pcoup.push_back(-temp); } else if(temp < 0){ ncoup.push_back(temp); } } cin>>np; for(int i = 0; i < np; i ++){ int temp; scanf("%d",&temp); if(temp > 0){ pprod.push_back(-temp); } else if(temp < 0){ nprod.push_back(temp); } } sort(pcoup.begin(),pcoup.end()); sort(ncoup.begin(),ncoup.end()); sort(pprod.begin(),pprod.end()); sort(nprod.begin(),nprod.end()); int i = 0; int sum = 0; while(i < pcoup.size() && i < pprod.size()){ sum += pcoup[i] * pprod[i]; i++; } i = 0; while(i < ncoup.size() && i < nprod.size()){ sum += ncoup[i] * nprod[i]; i++; } printf("%d",sum); return 0;}
新闻热点
疑难解答