PAT A1037 Magic Coupon

PAT A1037 Magic Coupon

在这里插入图片描述

Sample Input:

4
1 2 4 -1
4
7 6 -2 -3

Sample Output:

43
wordmeaning
couponsn.优惠券,礼券
bonusn. 奖金;红利
  • 思路 1:
    分别存储coupons和products的正数和负数,排序后遍历,对负数,从最小值开始遍历到一个数组结束,对正数从最大值开始,遍历到一个数组空

  • code 1:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> NC[3], NP[3];	//分别存放正数和负数 
int main(){
	int nc, np, tmp;
	scanf("%d", &nc);
	for(int i = 0; i < nc; ++i){
		scanf("%d", &tmp);
		if(tmp >= 0) NC[1].push_back(tmp);
		else NC[0].push_back(tmp);
	}
	scanf("%d", &np);
	for(int i = 0; i < np; ++i){
		scanf("%d", &tmp);
		if(tmp >= 0) NP[1].push_back(tmp);
		else NP[0].push_back(tmp);
	}
	for(int i = 0; i < 2; ++i){
		sort(NC[i].begin(), NC[i].end());
		sort(NP[i].begin(), NP[i].end());
	}
	int result = 0;
	int len1 = NC[0].size(), len2 = NP[0].size(), i = 0, j = 0;
	while(i < len1 && j < len2){
	//处理负数 
		result += NC[0][i] * NP[0][j];
		i++; j++;
	}
	i = NC[1].size()-1;
	j = NP[1].size()-1;
	while(i >= 0 && j >= 0){
		result += NC[1][i] * NP[1][j];
		i--; j--;
	}
	printf("%d", result);
	return 0;
} 
  • 思路 2:
    优化1,不需要两个vector存储,直接用数组即可,以>0或者<0为边界
  • code 2:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100010;
int NC[maxn], NP[maxn];	//分别存放正数和负数 
int main(){
	int nc, np, tmp;
	scanf("%d", &nc);
	for(int i = 0; i < nc; ++i){
		scanf("%d", &NC[i]);
	}
	scanf("%d", &np);
	for(int i = 0; i < np; ++i){
		scanf("%d", &NP[i]);
	}
	sort(NC, NC+nc);
	sort(NP, NP+np);
	int result = 0;
	int i = 0, j = 0;
	while(NC[i] < 0 && NP[j] < 0){
	//处理负数 
		result += NC[i] * NP[j];
		i++; j++;
	}
	i = nc-1; j = np-1;
	while(NC[i] > 0 && NP[j] > 0){
		result += NC[i] * NP[j];
		i--; j--;
	}
	printf("%d", result);
	return 0;
} 
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int coupons[maxn], products[maxn];

int main(){
	int nc, np, sum = 0;
	scanf("%d", &nc);
	for(int i = 0; i < nc; ++i){
		scanf("%d", &coupons[i]);
	}
	scanf("%d", &np);
	for(int i = 0; i < np; ++i){
		scanf("%d", &products[i]);
	}
	sort(coupons, coupons+nc);
	sort(products, products+np);
	for(int i = 0; coupons[i] < 0; ++i){
		if(products[i] < 0)  sum += coupons[i] * products[i];
 		else break;
	}
	for(int i = 0; coupons[nc-1-i] > 0; ++i){
		if(products[np-1-i]>0) sum += coupons[nc-1-i] * products[np-1-i];
		else break;
	}
	printf("%d", sum);
	return 0;
} 

  • T3 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
vector<int> num[2];
int main()
{
    int n;
    for(int i = 0; i < 2; ++i)
    {
        scanf("%d", &n);
        num[i].resize(n);
        for(int j = 0; j < n; ++j)
        {
            scanf("%d", &num[i][j]);
        }
        sort(num[i].begin(), num[i].end());
    }
    int sum = 0;
    for(int i = 0; num[0][i] < 0; ++i)
    {
        if(num[1][i] < 0) sum += num[0][i] * num[1][i];
        else break;
    }
    int len1 = num[0].size(), len2 = num[1].size();
    for(int i = 0; num[0][len1-1-i] > 0; ++i)
    {
        if(num[1][len2-1-i] > 0) sum += num[0][len1-1-i] * num[1][len2-1-i];
        else break;
    }
    printf("%d", sum);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值