PAT-A1037(C/C++代码解析)

1037 Magic Coupon (25 分)
PAT-A1037(C/C++代码解析)
PAT-A1037(C/C++代码解析)
注:贪心

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=888888;	
int a1[maxn]={0},a2[maxn]={0},b1[maxn]={0},b2[maxn]={0};
bool cmp (int a,int b)
{
	return a>b;
}

int main ()
{
    int sum=0,m,n;

	int i,a,b,numa1=0,numa2=0,numb1=0,numb2=0;
    scanf ("%d",&m);
    for (i=0;i<m;i++)
    {
    	scanf ("%d",&a);
    	if (a>=0)
    	a1[numa1++]=a;
    	else
    	a2[numa2++]=a;
	}
	scanf ("%d",&n);
	for (i=0;i<n;i++)
	{
		scanf ("%d",&b);
		if (b>=0)
		b1[numb1++]=b;
		else
		b2[numb2++]=b;
	}
	sort(a1,a1+maxn,cmp);
	sort(a2,a2+maxn);
	sort(b1,b1+maxn,cmp);
	sort(b2,b2+maxn);
	for (i=0;i<numa1&&i<numb1;i++)
	sum=sum+a1[i]*b1[i];
	for (i=0;i<numa2&&i<numb2;i++)
	sum=sum+a2[i]*b2[i];
	printf ("%d",sum);
    return 0;
}
上一篇:关于opencv基础知识入门30讲及代码


下一篇:机器学习|贝叶斯分类器