1037 Magic Coupon
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472
这个题目有毒,开始我的while判断是使用的相乘大于0这种判断方式,但是最后一个案例始终过不了,可能是因为越界了,但是越界的话,为什么可以直接相加呢?还是有点小疑问。代码如下:
1 #include<iostream> 2 #include<algorithm> 3 #include <sstream> 4 #include<cstring> 5 using namespace std; 6 bool cmp(int a,int b){ 7 return a > b; 8 } 9 string add(string a,string b){ 10 int n = a.size()-1; 11 int m = b.size()-1; 12 string ret = ""; 13 int jinwei = 0; 14 while(n != 0 && m != 0){ 15 if(((a[n]-'0')+(b[m]-'0')+jinwei) >= 10){ 16 ret.insert(0,(char*)(((a[n]-'0')+(b[m]-'0')+jinwei-10)+'0')); 17 jinwei = 1; 18 } 19 else{ 20 ret.insert(0,(char*)((a[n]-'0')+(b[m]-'0')+jinwei)+'0'); 21 jinwei = 0; 22 } 23 } 24 return ret; 25 } 26 int main(){ 27 int coupon[10001],product[100001]; 28 int n,m; 29 // string a = "123"; 30 // string b = "321"; 31 // cout << add(a,b); 32 cin >> n; 33 for(int i = 0; i < n; i++){ 34 cin >> coupon[i]; 35 } 36 sort(coupon,coupon+n,cmp); 37 cin >> m; 38 for(int j = 0; j < m; j++){ 39 cin >> product[j]; 40 } 41 sort(product,product+m,cmp); 42 int i = 0, j = 0; 43 // string ret = ""; 44 // string stem = ""; 45 // long long sum = coupon[i++]*product[j++]; 46 long long sum = 0; 47 // stringstream ss; 48 while(i < n && j < m && coupon[i] > 0 && product[j] > 0){ 49 // ss << sum; 50 // ss >> stem; 51 sum += coupon[i]*product[j]; 52 i++; 53 j++; 54 } 55 i = n-1,j = m-1; 56 while(i >= 0 && j >= 0 && coupon[i] < 0 && product[j] < 0){ 57 sum += coupon[i]*product[j]; 58 i--; 59 j--; 60 } 61 cout << sum; 62 //cout << product[0]; 63 return 0; 64 }