编程语言
首页 > 编程语言> > 用LUA(和C++)刷PAT (Advanced Level) ——1037 Magic Coupon

用LUA(和C++)刷PAT (Advanced Level) ——1037 Magic Coupon

作者:互联网

int的bit数是32,题目里明确标识可以int解决的事不要用double

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int Nc, Np;
    vector<int> C, P;
    int result = 0;
    cin>>Nc;

    for(int i = 0; i < Nc; i++){
        int d;
        cin>>d;
        C.push_back(d);
    }
    
    cin>>Np;
    
    for(int i = 0; i < Np; i++){
        int d;
        cin>>d;
        P.push_back(d);
    }
    
    sort(C.begin(), C.end());
    sort(P.begin(), P.end());
    
    auto Citr = C.begin(), Pitr = P.begin();
    while(Citr != C.end() && Pitr != P.end()  && *Citr < 0 && *Pitr < 0){
        result += (*Citr)*(*Pitr);
        Citr ++;
        Pitr ++;
    }
    
    reverse(C.begin(), C.end());
    reverse(P.begin(), P.end());

    Citr = C.begin(), Pitr = P.begin();
    while(Citr != C.end() && Pitr != P.end()  && *Citr > 0 && *Pitr > 0){
        result += (*Citr)*(*Pitr);
        Citr ++;
        Pitr ++;
    }
    
    cout<<result;
}

标签:begin,PAT,Level,Coupon,Citr,Pitr,int,&&,end
来源: https://blog.csdn.net/weixin_44316978/article/details/118864064