#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100010; //段错误,数组开辟的太小
int a[MAXN], b[MAXN];
int main(){
int n1, n2;
scanf("%d", &n1);
for(int i = 0; i < n1; i++){
scanf("%d", &a[i]);
}
scanf("%d", &n2);
for(int i = 0; i < n2; i++){
scanf("%d", &b[i]);
}
sort(a, a + n1); //从小到大排序
sort(b, b + n2); //从小到大排序
int i = 0, j, p_sum = 0;
while(i < n1 && i < n2 && a[i] < 0 && b[i] < 0){
p_sum += a[i] * b[i]; //当前位置小于0,累加乘积
i++;
}
i = n1 - 1;
j = n2 - 1;
while(i >= 0 && j >= 0 && a[i] > 0 && b[j] > 0){
p_sum += a[i] * b[j]; //当前位置均大于0,累加乘积
i--;
j--;
}
printf("%d", p_sum);
return 0;
}
原文地址:https://www.cnblogs.com/zjsaipplp/p/10425234.html
时间: 2024-11-09 04:16:09