如何找到两个不同数组中交换的值?

How to find the values being swapped in two different arrays?

我想交换一个数组,一个数组中的赔率和另一个数组中的偶数同时跟踪计数和被交换的值。

{2, 3, 6}
{1, 4, 7}

will become

{1, 3, 7}
{2, 4, 6}

有 2 次交换,交换的值是 1 &2, 6 & 7.

int main() {
    int a1[3] = { 2, 3, 6 };
    int a2[3] = { 1, 4, 7 };
    int i;
    int swapcount = 0;
    int swapvalue;

    std::cout << "Before swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        cout << " " << a1[i] << endl;

    }
    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    for (int i = 0; i < 3; i++) {
        if (a1[i] % 2 != 1) {
            swapcount++;
            int temp = a1[i];
            a1[i] = a2[i];
            a2[i] = temp;
            swapvalue = i;
        }
    }

    std::cout << "After swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a1[i] << endl;
    }

    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    std::cout << "swap count: " << swapcount << endl;
    std::cout << "swap value: " << swapvalue << endl;
}

到目前为止,我已经让 swapcounter 正常工作,但我似乎无法弄清楚:

如何查找和存储交换元素的各个值? (我只能显示一个值。)

我能得到任何关于如何获取所有值的提示吗?除了输入和输出流,我不允许使用任何额外的库。提前致谢。

有很多方法可以做到这一点。最简单的解决方案是将它们存储在 std::vector of std::pair:

std::vector<std::pair<int, int>> swapvalues;

然后插入:

swapvalues.emplace_back(val1, val2);

顺便说一下,在您的代码中存储的是索引,而不是值。如果要存储索引,请仅使用 std::vector 个整数。

How to find the individual values of the elements that are swapped?

交换元素时存储它们的索引:

 int swappedValuesIndex[];

 for (int i = 0; i < 3; i++) {
    if (a1[i] % 2 != 1) {
        // store swapped values' index  
        swappedValuesIndex[swapcount] = i;

        swapcount++;

        int temp = a1[i];
        a1[i] = a2[i];
        a2[i] = temp;
        swapvalue = i;
    }
 } 

 // print swapped values
 for (int i = 0; i < swapcount; i++){
     cout <<"a1["<< swappedValuesIndex[i] <<"]"<< a1[swappedValuesIndex[i]] <<'\n';
     cout <<"a2["<< swappedValuesIndex[i] <<"]"<< a2[swappedValuesIndex[i]] <<'\n';
 }

创建一个辅助数组swapbool,如果交换完成则设置boolean = 1。

#include<iostream>
using namespace std;
int main() {
    int a1[3] = { 2, 3, 6 };
    int a2[3] = { 1, 4, 7 };
    int swapbool[3] = {0,0,0};
    int i;
    int swapcount = 0;
    int swapvalue;

    std::cout << "Before swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        cout << " " << a1[i] << endl;

    }
    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    for (int i = 0; i < 3; i++) {
        if (a1[i] % 2 != 1) {
            swapcount++;
            int temp = a1[i];
            a1[i] = a2[i];
            a2[i] = temp;
           // swapvalue = i;
            swapbool[i]=1;
        }
    }

    std::cout << "After swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a1[i] << endl;
    }

    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    std::cout << "swap count: " << swapcount << endl;
    for (int i = 0; i < 3; i++) {
        if(swapbool[i] == 1)
        {
            std::cout << "swapvalue1-> " << a2[i] << endl;
            std::cout << "swapvalue2-> " << a1[i] << endl;
        }
    }

    //std::cout << "swap value: " << swapvalue << endl;
}