以下是两个数组的和积的代码,在某些语句中有一些错误(可能导致错误的输出)。选择具有错误语句的正确选项。
// CPP program to find minimum sum of product
// of two arrays with k operations allowed on first array.
#include
using namespace std;
// Function to find the minimum product
int minproduct(int a[], int b[], int n, int k)
{
int diff = 0, res = 0;
int temp;
for (int i = 0; i < n; i++) {
// Find product of current elements and update result.
int pro = a[i] * b[i];
res = res + pro;
// If both product and b[i] are negative,
// we must increase value of a[i] to minimize result.
if (pro < 0 && b[i] < 0)
temp = (a[i] + 2 * k) * b[i];
// If both product and a[i] are negative,
// we must decrease value of a[i] to minimize result.
else if (pro < 0 && a[i] < 0)
temp = (a[i] - 2 * k) * b[i];
// Similar to above two cases for positive product.
else if (pro > 0 && a[i] < 0)
temp = (a[i] + 2 * k); // statement 1
else if (pro > 0 && a[i] > 0)
temp = (a[i] - 2 * k); // statement 2
// Check if current difference becomes higher
// than the maximum difference so far.
int d = abs(pro - temp);
if (d > diff)
diff = d;
}
return res - diff;
}
// Driver function
int main()
{
int a[] = { 2, 3, 4, 5, 4 };
int b[] = { 3, 4, 2, 3, 2 };
int n = 5, k = 3;
cout << minproduct(a, b, n, k)
<< endl;
return 0;
}
(A)仅陈述1
(B)仅陈述2
(C)陈述1和2
(D)陈述1和陈述2均不答案: (C)
说明:请参阅:https://www.geeksforgeeks.org/minimum-sum-product-two-arrays/
这个问题的测验