怎么用编程方法来表现实际问题?
本帖最后由 执迷不悟0527 于 2022-4-14 12:55 编辑求两个集合的交、并、补、差。
如果用编程方法的话该怎么写呢??求解 数字存进数组,然后循环判断。不知道C库有没有专门的函数,没有的话就自己写吧。 C 没有,要自己写。C++ 有 set 库 zzxhh628 发表于 2022-4-14 12:46
数字存进数组,然后循环判断。不知道C库有没有专门的函数,没有的话就自己写吧。
能详细的说一下嘛?或者能直接告诉我代码吗?麻烦你了up主 傻眼貓咪 发表于 2022-4-14 13:07
C 没有,要自己写。C++ 有 set 库
能详细的说一下嘛?或者能直接告诉我代码吗?麻烦你了up主 你可以写一个函数,参数是两个数组,就是用for循环和if语句判断符不符合∩一类的条件,符合的然后存到另一个数组里,有必要进行顺序排序,然后把结果传出去就行了。具体代码网上有很多,我有时间再写吧。 zzxhh628 发表于 2022-4-14 13:26
你可以写一个函数,参数是两个数组,就是用for循环和if语句判断符不符合∩一类的条件,符合的然后存到另一 ...
好的,如果你有时间能写一下就完美了 ,谢谢你了~
执迷不悟0527 发表于 2022-4-14 13:28
好的,如果你有时间能写一下就完美了 ,谢谢你了~
C++#include <iostream>
#include <set>
void print(std::string str, std::set<int> set){
std::cout << str << ": ";
for(const int& n: set){
std::cout
<< n
<< " ";
}
std::cout << std::endl;
}
int main(){
std::set<int> x = {3, 12, 5, 6};
std::set<int> y = {6, 10, 5, 1, 7};
print("x集", x);
print("y集", y);
std::set<int> a;
std::set<int> b;
std::set<int> c1;
std::set<int> c2;
std::set<int> d;
std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(a, a.begin())); // 交集
std::set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(b, b.begin())); // 合集
std::set_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(c1, c1.begin())); // 差
std::set_difference(y.begin(), y.end(), x.begin(), x.end(), inserter(c2, c2.begin())); // 差
std::set_symmetric_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(d, d.begin())); // 对称差
print("x和y的交集", a);
print("x和y的合集", b);
print("x差y的集", c1);
print("y差x的集", c2);
print("x和y的对称差集", d);
return 0;
}x集: 3 5 6 12
y集: 1 5 6 7 10
x和y的交集: 5 6
x和y的合集: 1 3 5 6 7 10 12
x差y的集: 3 12
y差x的集: 1 7 10
x和y的对称差集: 1 3 7 10 12 傻眼貓咪 发表于 2022-4-14 13:31
C++
你好博主,我把您的代码放在devc++里面为什么显示运行错误呢? 文件后缀 .cpp 傻眼貓咪 发表于 2022-4-14 15:10
文件后缀 .cpp
是cpp的后缀为什么还是不对呢??? 执迷不悟0527 发表于 2022-4-15 09:49
是cpp的后缀为什么还是不对呢???
请问报错什么? 执迷不悟0527 发表于 2022-4-15 09:49
是cpp的后缀为什么还是不对呢???
试试多加这两行:#include <algorithm>
#include <iterator> 傻眼貓咪 发表于 2022-4-15 10:13
试试多加这两行:
range-based 'for' loops are not allowed in C++98 mode
In function 'int main()':
in C+ +98 'x' must be initialized by constructor, not by "(..…"
could not convert '(3, 12, 5, 6' from '<brace-enclosed initializer list>' to 'std:set<int> in C++98'y' must be initialized by constructor, not by '(...”
could not convert '(6,10, 5, 1, 7Y from'<brace-enclosed initializer list>' to 'std:setsint>
这是全部报错了~~~~ 执迷不悟0527 发表于 2022-4-15 10:24
range-based 'for' loops are not allowed in C++98 mode
In function 'int main()':
...
版本太旧了,C++ 98 (1998 年)基本没有人再用了,试试更新吧,我用的是 C++20 在这里插个眼,暂时加不了好友,怕一会找不到大神
页:
[1]