This is the requirement of the problem:
S is a set of n positive integers and n is even. Please design an efficient algorithm to divide S into two subsets S1 and S2, such that each subset contains n/2 elements, and the difference between the sum of all elements in S1 and the sum of all elements in S2 is maximum
Input:
2
22
68 25 34 16 2 37 3 95 76 57 21 13 4 78 29 6 17 39 51 20 43 12
26
28 3 48 59 14 32 47 51 42 61 9 24 52 78 65 2 37 78 51 73 29 7 26 95 37 2
2 3 4 6 12 13 16 17 20 21 25
29 34 37 39 43 51 57 68 76 78 95
2 2 3 7 9 14 24 26 28 29 32 37 37
42 47 48 51 51 52 59 61 62 65 65 73 78 95
This is my code:
```c
#include <stdio.h>
int main(){
int m=0;
scanf("%d\n",&m);
while(m>0){
int n,i,j,t=0;
scanf("%d\n",&n);
int a[n];
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++){
for(j=1;j<=n-i;j++){
if(a[j+1]<a[j]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=1;i<=n/2;i++){
printf("%d ",a[i]);
}
printf("\n");
for(i=n/2+1;i<=n;i++){
printf("%d ",a[i]);
}
printf("\n");
printf("\n");
m--;
}
}
My idea is to sort the data of each group and divide it into two small subsets from the middle. However, it is not possible to input multiple sets of data at once and output the results in turn as required by the problem.
0 Answer
No answer yet
这家伙很懒,什么都没留下...