1.请编写一个函数,筛选出下面2个一维数组中,不同的的值。 Int a[5] ={1,2,3,4,5}; Int b[5] ={5,4,6,3,7};
时间: 2023-08-19 15:07:56 浏览: 43
可以使用两个for循环嵌套进行比较,将不同的值存储到一个新的数组中。
以下是示例代码:
```c++
#include <iostream>
using namespace std;
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {5, 4, 6, 3, 7};
int diff[10];
int count = 0;
for (int i = 0; i < 5; i++) {
bool found = false;
for (int j = 0; j < 5; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
if (!found) {
diff[count] = a[i];
count++;
}
}
for (int i = 0; i < 5; i++) {
bool found = false;
for (int j = 0; j < 5; j++) {
if (b[i] == a[j]) {
found = true;
break;
}
}
if (!found) {
diff[count] = b[i];
count++;
}
}
cout << "Different values: ";
for (int i = 0; i < count; i++) {
cout << diff[i] << " ";
}
return 0;
}
```
输出结果为:Different values: 1 2 6 7