matlab拔河比赛_拔河比赛
matlab拔河比賽
Description:
描述:
This is a standard interview problem to divide a set of number to two different set where both the subset contains same number of element and have a minimum difference of sum between them using backtracking.
這是一個標準的采訪問題,它是將一組數字劃分為兩個不同的集合,其中兩個子集都包含相同數量的元素,并且使用回溯,它們之間的總和差異最小。
Problem statement:
問題陳述:
There is a set contains N number of elements. We have to divide this set into two different sets where each of the subsets contains the same number of elements and has a minimum difference between them.
有一個包含N個元素的集合。 我們必須將此集合分為兩個不同的集合,其中每個子集包含相同數量的元素,并且它們之間的差異最小。
If N is even then each of the subsets will contain N/2 number of elements.
如果N為偶數,則每個子集將包含N / 2個元素。
If N is odd then one of the subsets will contain (N+1)/2 and others will contain (N-1)/2 number of elements.
如果N為奇數,則子集中的一個子集將包含(N + 1)/ 2個元素,而其他子集將包含(N-1)/ 2個元素。
Example
例
T=3N=31 2 3Output: set1: {1, 2} set2: {3}N=41 2 3 4Output: set1: {1, 4} set2: {2, 3}N=101 4 8 6 -7 -10 87 54 16 100Output: set1: {{ 4, -7, -10, 87, 54 } set2: {1, 8, 6, 16, 100 }Explanation with example
舉例說明
Let, there is N no. of values, say e1, e2, e3, ..., en and N is even.
讓我們沒有N。 e 1 ,e 2 ,e 3 ,...,e n和N的值是偶數。
Subset1: { N/2 no. of values }Subset2: { N/2 no. of values }The process to insert the elements to the Subsets is a problem of combination and permutation. To get the result we use the backtracking process. Here we take the sum of all the elements
. Here we have to find out the elements that are in the subset no.1 and the elements that are not in subset1, must be in the subset no. 2. Every time we consider two things.將元素插入子集的過程是組合和排列的問題。 為了獲得結果,我們使用了回溯過程。 這里我們取所有元素的總和
The ith element is a part of the subset1.
第i 個元素是subset1的一部分。
The ith element is not a part of the subset1.
第i 個元素不是subset1的一部分。
After getting N/2 number of elements into the subset1. We will check,
在將N / 2數量的元素放入子集1之后 。 我們會檢查
If the value is less than then Difference we update the value of the Difference and make a note of the elements that are in the subset1.
如果該值小于那么差 ,我們更新之差的值并記錄是在SUBSET1元素。
After getting the elements for those the value of Difference is less we check the elements of the main set and find out the elements that are not in the subset1 and put them in subset2.
獲得元素對于那些經過差值小于我們檢查的主要集合中的元素,并找出不在SUBSET1的元素,并把它們放在SUBSET2。
Set: {1, 2, 3}Sum=61 is in the subset1 therefore Subset1: {1} and Difference = abs(3-1) = 2
1在子集1中,因此子集1:{1},并且差= abs(3-1)= 2
2 is in the subset1 therefore Subset1: {1,2} and Difference = abs(3-3) = 0
2在子集1中,因此子集1:{1,2},且差= abs(3-3)= 0
Therefore, Difference is minimum.
因此, 差異最小。
Subset1: {1,2}Subset2: {3}C++ implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;void traverse(int* arr, int i, int n, int no_of_selected, int sum, int& diff, bool* take, vector<int>& result, vector<int> current, int current_sum) {//if the current position is greater than or equals to nif (i >= n)return;//if the value of difference is greater than//Sum/2-current sum of the elements of Subset no.1if ((diff > abs(sum / 2 - current_sum)) && (no_of_selected == (n + 1) / 2 || no_of_selected == (n - 1) / 2)) {diff = abs(sum / 2 - current_sum);//store the subset no. 1result = current;}//taking the elemets after the current element one by onefor (int j = i; j < n; j++) {take[j] = true;current.push_back(arr[j]);traverse(arr, j + 1, n, no_of_selected + 1, sum, diff, take, result, current, current_sum + arr[j]);current.pop_back();take[j] = false;} }void find(int* arr, int n) {//array to distinguished the elements those are in subset no. 1bool take[n];int sum = 0;for (int i = 0; i < n; i++) {sum += arr[i];take[i] = false;}int diff = INT_MAX;int no_of_selected = 0;vector<int> result;vector<int> current;int current_sum = 0;int i = 0;traverse(arr, i, n, no_of_selected, sum, diff, take, result, current, current_sum);set<int> s;//elements those are in subset no.1cout << "Set1 : { ";for (int j = 0; j < result.size(); j++) {cout << result[j] << " ";s.insert(result[j]);}cout << "}" << endl;//elements those are in subset no.2cout << "Set2 : { ";for (int j = 0; j < n; j++) {if (s.find(arr[j]) == s.end()) {cout << arr[j] << " ";}}cout << "}" << endl; }int main() {int t;cout << "Test case : ";cin >> t;while (t--) {int n;cout << "Enter the value of n : ";cin >> n;int arr[n];cout << "Enter the values: ";//taking the set elementsfor (int i = 0; i < n; i++) {cin >> arr[i];}find(arr, n);}return 0; }Output
輸出量
Test cases : 3 Enter the value of n : 3 Enter the values: 1 2 3 Set1 : { 1 2 } Set2 : { 3 } Enter the value of n : 4 Enter the values: 1 2 3 4 Set1 : { 2 3 } Set2 : { 1 4 } Enter the value of n : 10 Enter the values: 1 4 8 6 -7 -10 87 54 16 100 Set1 : { 4 -7 -10 87 54 } Set2 : { 1 8 6 16 100 }翻譯自: https://www.includehelp.com/icp/tug-of-war.aspx
matlab拔河比賽
總結
以上是生活随笔為你收集整理的matlab拔河比赛_拔河比赛的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java LinkedList公共布尔提
- 下一篇: 更快的Maven来了,我的天,速度提升了