PAT 甲级 A1037 (2019/02/20)

#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

PAT 甲级 A1037 (2019/02/20)的相关文章

PAT 甲级 A1010 (2019/02/20)

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL Map[256]; // 0 ~ 9, a ~ z 与 0 ~ 35 的对应 LL Y = 1; // 定义LL型的Y LL inf = (1LL << 63) - Y; // long long 的最大值 2^63 - 1,注意括号 void init

PAT 甲级 A1044 (2019/02/20)

#include<cstdio> const int MAXN = 100010; int sum[MAXN]; int n, S, nearS = 100000010; int upper_bound(int L, int R, int x) { int left = L, right = R, mid; while(left < right) { mid = (left + right) / 2; if(sum[mid] > x) { right = mid; } else {

PAT 甲级 A1067 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int a[MAXN]; //存放各个数字当前所处的位置编号 int main(){ int n, num, ans = 0;//表示总计交换次数 scanf("%d", &n); int surplus = n - 1;//存放除0以外不在本位上的数的个数 for(int i = 0; i

PAT 甲级 A1085 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int n, p, a[MAXN]; int binarySearch(int i, long long x){ if(a[n - 1] <= x) //如果最大的数比x小,则返回n return n; int left = i + 1; int right = n - 1; //在区间[i+1, n-1]内查找

PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone c

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if