首页 > 代码库 > 1037. Magic Coupon (25)

1037. Magic Coupon (25)

 1 #include<iostream> 2 #include<vector> 3 #include<stdio.h> 4 #include<algorithm> 5 using namespace std; 6  7 int cmp(int a,int b) 8 { 9    return a<b;10 }11 12 int main()13 {14     int n,i,j,tem;15     while(scanf("%d",&n)!=EOF)16     {17         getchar();18         vector<int> v1;19         for(i=0;i<n;i++)20         {21             scanf("%d",&tem);22             v1.push_back(tem);23         }24         getchar();25         sort(v1.begin(),v1.end(),cmp);26 27         scanf("%d",&n);28         getchar();29         vector<int> v2;30         for(i=0;i<n;i++)31         {32             scanf("%d",&tem);33             v2.push_back(tem);34         }35         sort(v2.begin(),v2.end(),cmp);36         int sum = 0;37         i=0;38         while(i<v1.size()&&i<v2.size()&&v1[i]<0&&v2[i]<0)39         {40                 sum+=(v1[i]*v2[i]);41                 i++;42         }43         44 45 46             i=v1.size()-1;j=v2.size()-1;47 48             while(i>=0&&j>=0&&v1[i]>0&&v2[j]>0)49             {50         51                    sum+=(v1[i]*v2[j]);52                    --i;53                    --j;54             }55 56             printf("%d\n",sum);57         }58         59     return 0;60 }

 

1037. Magic Coupon (25)