如何使用递归函数调用在C++中对偶数长度整数数组的每个元素进行配对?

huangapple go评论63阅读模式
英文:

How to pair every element of an even-length integer array in C++ using recursive function calls?

问题

Here's the translated code portion you requested:

所以目前我有一个长度为偶数的数组 `int arr[]`。我的问题是,我试图将每个元素与另一个元素配对到一个映射 `unordered_map <int, int> pairs` 中。只要 `arr[]` 的大小大于2,就会有多个可能性。我需要生成所有可能的配对集。

为了澄清这个目标,这里有一个示例:

```cpp
int arr[4] = {1, 2, 3, 4};

如果这是数组,那么映射可以有以下配对:

pairs = {{1, 2}, {2, 1}, {3, 4}, {4, 3}};//1 和 2, 3 和 4
pairs = {{1, 3}, {3, 1}, {2, 4}, {4, 2}};//1 和 3, 2 和 4
pairs = {{1, 4}, {4, 1}, {2, 3}, {3, 2}};//1 和 4, 2 和 3

对于这类问题,我自动选择了递归方法,通过无限嵌套的循环来处理每对元素,标记它们为已配对,并进入下一层循环。然而,一些尝试表明这个任务超出了我的能力。有人可以帮我吗?

我的代码:

#include <iostream>
#include <deque>
#include <unordered_map>
int n = 4;
std::unordered_map <int, int> pairs;
std::deque <int> arr = {0, 1, 2, 3};
std::deque <bool> paired = {false, false, false, false};
bool finishedPairing() {
    for (int i = 0; i < n; i++) {
        if (paired[i] == false) { return false; }
    }
    return true;
}
void pairUp() {
    if (finishedPairing()) {
        std::cout << "\n新集合:\n";
        for (int i = 0; i < 4; i++) {
            std::cout << i << " 配对:" << pairs[i] << '\n';
        }
        return;
    }
//需要写成C++代码的部分
}
int main () {
    pairUp();
    return 0;
}

我手动运行了 pairUp() 后应该得到的输出是:

新集合:
0 配对:1
1 配对:0
2 配对:3
3 配对:2

新集合:
0 配对:2
1 配对:3
2 配对:0
3 配对:1

新集合:
0 配对:3
1 配对:2
2 配对:1
3 配对:0

<details>
<summary>英文:</summary>

So currently I have an array `int arr[]` of a length that is an even number. My problem is that I am trying to pair each element up with another into a map `unordered_map &lt;int, int&gt; pairs`. As long as `arr[]` has a size that is greater than 2, there will be multiple possibilities. What I need is to generate all possible sets of pairings.

To clarify this objective, here is an example:

int arr[4] = {1, 2, 3, 4};

If that is the array, then the map could have these pairs:

pairs = {{1, 2}, {2, 1}, {3, 4}, {4, 3}};//1 and 2, 3 and 4
pairs = {{1, 3}, {3, 1}, {2, 4}, {4, 2}};//1 and 3, 2 and 4
pairs = {{1, 4}, {4, 1}, {2, 3}, {3, 2}};//1 and 4, 2 and 3


For these types of problems, I automatically went for the recursive approach to have an indefinite number of nested loops going through each pair, marking them as already paired, and going into the next level of loops. However, some tries proved that this task is beyond my capabilities. Can anyone help me here?

My code:

#include <iostream>
#include <deque>
#include <unordered_map>
int n = 4;
std::unordered_map <int, int> pairs;
std::deque <int> arr = {0, 1, 2, 3};
std::deque <bool> paired = {false, false, false, false};
bool finishedPairing() {
for (int i = 0; i < n; i++) {
if (paired[i] == false) { return false; }
}
return true;
}
void pairUp() {
if (finishedPairing()) {
std::cout << "\nNew Set:\n";
for (int i = 0; i < 4; i++) {
std::cout << i << " is paired with " << pairs[i] << '\n';
}
return;
}
//what I need written as C++ code
}
int main () {
pairUp();
return 0;
}

The output I should get after I did `pairUp()` by hand is:

New Set:
0 is paired with 1
1 is paired with 0
2 is paired with 3
3 is paired with 2

New Set:
0 is paired with 2
1 is paired with 3
2 is paired with 0
3 is paired with 1

New Set:
0 is paired with 3
1 is paired with 2
2 is paired with 1
3 is paired with 0



</details>


# 答案1
**得分**: 0

以下是您要翻译的代码部分:

```cpp
The solution was discovered when I realized I could pick the first non-paired number for each recursive run and cycle through the other possible second terms to pair the first up with and repeat until everything is paired. Also, there was an error. Since the elements in `arr[]` might not be numbered starting from 0 and increasing by one, some `i` and `j` in the code should become `arr[i]` and `arr[j]`.

The full piece:
#include &lt;iostream&gt;
#include &lt;deque&gt;
#include &lt;unordered_map&gt;
using namespace std;
int n = 4;
std::unordered_map &lt;int, int&gt; pairs;
std::deque &lt;int&gt; arr = {0, 1, 2, 3};
std::deque &lt;bool&gt; paired = {false, false, false, false};
bool finishedPairing() {
    for (int i = 0; i &lt; n; i++) {
        if (paired[i] == false) { return false; }
    }
    return true;
}
void pairUp() {
    if (finishedPairing()) {
        std::cout &lt;&lt; &quot;\nNew Set:\n&quot;;
        for (int i = 0; i &lt; 4; i++) {
            std::cout &lt;&lt; arr[i] &lt;&lt; &quot; is paired with &quot; &lt;&lt; pairs[arr[i]] &lt;&lt; &#39;\n&#39;; //changed i into arr[i]
        }
        return;
    }
//add portion:
    int i = 0;
    while (paired[i] == true) { i++; } //find first non-paired
    paired[i] = true;//pair it
    for (int j = 0; j &lt; n; j++) {
        if (paired[j] == false) { //find a counterpart
            paired[j] = true, pairs[arr[i]] = arr[j], pairs[arr[j]] = arr[i];//pair it
            pairUp();//next recursive run
            paired[j] = false;//reset
        }
    }
    paired[i] = false;//reset
}
int main () {
    pairUp();
    return 0;
}

测试结果:

New Set:
0 is paired with 1
1 is paired with 0
2 is paired with 3
3 is paired with 2

New Set:
0 is paired with 2
1 is paired with 3
2 is paired with 0
3 is paired with 1

New Set:
0 is paired with 3
1 is paired with 2
2 is paired with 1
3 is paired with 0
英文:

The solution was discovered when I realized I could pick the first non-paired number for each recursive run and cycle through the other possible second terms to pair the first up with and repeat until everything is paired. Also, there was an error. Since the elements in arr[] might not be numbered starting from 0 and increasing by one, some i and j in the code should become arr[i] and arr[j].

The full piece:

#include &lt;iostream&gt;
#include &lt;deque&gt;
#include &lt;unordered_map&gt;
using namespace std;
int n = 4;
std::unordered_map &lt;int, int&gt; pairs;
std::deque &lt;int&gt; arr = {0, 1, 2, 3};
std::deque &lt;bool&gt; paired = {false, false, false, false};
bool finishedPairing() {
    for (int i = 0; i &lt; n; i++) {
        if (paired[i] == false) { return false; }
    }
    return true;
}
void pairUp() {
    if (finishedPairing()) {
        std::cout &lt;&lt; &quot;\nNew Set:\n&quot;;
        for (int i = 0; i &lt; 4; i++) {
            std::cout &lt;&lt; arr[i] &lt;&lt; &quot; is paired with &quot; &lt;&lt; pairs[arr[i]] &lt;&lt; &#39;\n&#39;; //changed i into arr[i]
        }
        return;
    }
//add portion:
    int i = 0;
    while (paired[i] == true) { i++; } //find first non-paired
    paired[i] = true;//pair it
    for (int j = 0; j &lt; n; j++) {
        if (paired[j] == false) { //find a counterpart
            paired[j] = true, pairs[arr[i]] = arr[j], pairs[arr[j]] = arr[i];//pair it
            pairUp();//next recursive run
            paired[j] = false;//reset
        }
    }
    paired[i] = false;//reset
}
int main () {
    pairUp();
    return 0;
}

Test results:


New Set:
0 is paired with 1
1 is paired with 0
2 is paired with 3
3 is paired with 2

New Set:
0 is paired with 2
1 is paired with 3
2 is paired with 0
3 is paired with 1

New Set:
0 is paired with 3
1 is paired with 2
2 is paired with 1
3 is paired with 0

huangapple
  • 本文由 发表于 2023年5月22日 04:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定