[Programming question] There are two sequences A and B, all of which are N in size, and the values of the sequence elements are random integers and unordered. (You need to study them later)

Source: Internet
Author: User

32. (array, Planning)
There are two sequences, A and B, all of which are n. The values of the sequence elements are random integers and unordered;
Requirement: by exchanging elements in A and B, the deviation between [Sum of Element A] and [Sum of Element B] is minimized.
For example:
VaR A = [, 99,];
VaR B = [1, 2, 3, 4, 5, 40];

 

First, the goal must be to first find n numbers to make the number and half smaller than the sum, but the closest.

Idea 1: I started to look at this question, which is similar to the previous dynamic planning. I wanted to use dynamic planning to solve it. However, I can't do it.

Thought 2: Then I want to write recursion, that is, to put all the numbers in A and B (n each) in C, and to select n digits in half, we can divide them:

1. Select 2n-1 for the last number and n-1 for the remaining number.

2. Select n numbers from the remaining number instead of the last number.

In this way, the idea of recursion is ready.

However, I made another mistake in how to transmit the obtained results. Each number is recursively selected. How can this problem be achieved ?? Okay, it's stuck here ......

Void choose (int * C, int Len, int N, int sum) // n is to select several numbers from It {static int sumtmax = 0; static int SUMT = 0; static int num = 0; If (LEN <n) {return;} If (n = 0) {If (SUMT <= sum/2 & SUMT> sumtmax) {sumtmax = SUMT;} printf ("% d: % d \ n", ++ num, SUMT, sumtmax); return;} choose (C, len-1, n, sum); SUMT + = C [Len-1]; choose (C, len-1, n-1, sum ); SUMT-= C [Len-1];} void Exchange (int * a, int * B, int Len) {int * c = (int *) malloc (2 * Len * sizeof (INT); int * smaller = (int *) malloc (LEN * sizeof (INT); int sum = 0; for (INT I = 0; I <Len; I ++) {C [I] = A [I]; sum + = A [I];} for (INT I = 0; I <Len; I ++) {C [I + Len] = B [I]; sum + = B [I];} choose (C, Len * 2, Len, sum );}

 

Train of Thought 3: The idea is the same as that of two columns for 12 people. The binary value is used to change from 0 to (1 <2 * n). 1 In the binary value indicates that the number is selected as, we traverse all the combinations that may find the nearest half of. You can use another number to save the distribution when it is closest.

However, the limit for writing this method is that the number of digits of the int is limited and only 64 bits are used. That is to say, when n is greater than 32, my method becomes invalid.

/* 32. (array and Planning) There are two sequences A and B, all of which are N in size, and the values of the sequence elements are random integers and unordered. Requirement: by exchanging elements in A and B, minimize the difference between [Sum of Element A] and [Sum of Element B. For example, var a = [, 99,]; var B = [,]; */# include <stdio. h> # include <stdlib. h> # include <string. h> int bit_c (int n) // In the Binary Expression of statistical N, there are several 1 {int result = 0; For (; n; N & = n-1, result ++ ); return result;} void exchange2 (int * a, int * B, int Len) {int * c = (int *) malloc (2 * Len * sizeof (INT )); int * smaller = (int *) malloc (LEN * sizeof (INT); int sum = 0; For (INT I = 0; I <Len; I ++) {c [I] = A [I]; sum + = A [I] ;}for (INT I = 0; I <Len; I ++) {c [I + Len] = B [I]; sum + = B [I];} int maxsumh = 0; int mask = 0; For (INT I = 0; I <(1 <Len * 2); I ++) {If (bit_c (I) = Len) {int sumhalf = 0; For (Int J = 0; j <Len * 2; j ++) {If (I> J) & 1) = 1) {sumhalf + = C [J];} if (sumhalf <= sum/2 & sumhalf> maxsumh) {maxsumh = sumhalf; mask = I ;}} int AI = 0, Bi = 0; for (Int J = 0; j <Len * 2; j ++) {If (mask> J) & 1) = 1) {A [ai ++] = C [J];} else {B [bi ++] = C [J] ;}} printf ("minimum difference: % d ", Sum-2 * maxsumh); free (c) ;}int main () {int A [5] = {1, 2, 3, 4, 5 }; int B [5] = {30, 40, 50, 60, 23}; exchange2 (A, B, 5); Return 0 ;}

 

There are various solutions on the Internet:

Two types of http://blog.csdn.net/tianshuai1111/article/details/7479767 are proposed.

But one of them was rejected by the http://blog.csdn.net/cwqbuptcwqbupt/article/details/7521733 with a counterexample.

The backtracking method is used in the http://blog.csdn.net/ljsspace/article/details/6434621#, but the comment says there is a problem I haven't looked at it carefully

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.