The problem is described to two groups of numbers, each n.
Adjust the order of the numbers in each group so that the two sets of data are multiplied by the same subscript elements, and then added and minimized. Requires the program to output this minimum value.
For example, two groups of numbers are: 1 3-5 and-2 4 1
Then the minimum value corresponding to the sum of products should be:
(-5) * 4 + 3 * (-2) + 1 * 1 =-25 input Format the first line one number T represents the number of data groups. After each set of data, first read into an n, the next two rows per row n number, the absolute value of each number is less than or equal to 1000.
n<=8,t<=1000 output Format A number represents the answer. Sample input
231 3-5-2 4 151 2 3 4 51 0 1 0 1
Sample output
-25
6
The code is as follows:
/* Minimum product (basic type):
Idea: Loop through two one-dimensional arrays, one ascending row, one descending row,
Multiply the number of the corresponding subscript and then add it, and the output will be. */
#include <stdio.h>
int main () {
int t,n;
int sum=0;
scanf ("%d", &t);//cyclic input of several sets of test data
int result[t];
for (int k=0;k<t;k++) {
scanf ("%d", &n);
int a[n],b[n];
Input array A[i]
for (int i=0; i<n; i++) {
scanf ("%d", &a[i]);
}
for (int i=0; i<n-1; i++) {
for (int j=0; j<n-1-i; J + +) {
if (A[j]<a[j+1]) {
int t = a[j];
A[J] = a[j+1];
A[j+1] = t;
}
}
}
Input array B[i]
for (int i=0; i<n; i++) {
scanf ("%d", &b[i]);
}
for (int i=0; i<n-1; i++) {
for (int j=0; j<n-1-i; J + +) {
if (B[j]>b[j+1]) {
int t = b[j];
B[J] = b[j+1];
B[j+1] = t;
}
}
}
/* Assigns A, b to the subscript multiplied by and as an element to the result array */
Result[k] = 0;
for (int i=0;i<n;i++) {
Result[k] + = A[i]*b[i];
}
}
for (int k=0;k<t;k++) {
printf ("%d\n", Result[k]);
}
}
C language · Minimum product (basic type)