鱼C论坛

 找回密码
 立即注册
查看: 2470|回复: 15

[已解决]怎么用编程方法来表现实际问题?

[复制链接]
发表于 2022-4-14 12:19:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 执迷不悟0527 于 2022-4-14 12:55 编辑

求两个集合的交、并、补、差。
如果用编程方法的话该怎么写呢??求解
最佳答案
2022-4-14 13:31:59
执迷不悟0527 发表于 2022-4-14 13:28
好的,如果你有时间能写一下就完美了 ,谢谢你了~


C++
  1. #include <iostream>
  2. #include <set>

  3. void print(std::string str, std::set<int> set){
  4.         std::cout << str << ": ";
  5.         for(const int& n: set){
  6.                 std::cout
  7.                         << n
  8.                         << " ";
  9.         }
  10.         std::cout << std::endl;
  11. }

  12. int main(){
  13.         std::set<int> x = {3, 12, 5, 6};
  14.         std::set<int> y = {6, 10, 5, 1, 7};
  15.        
  16.         print("x集", x);
  17.         print("y集", y);
  18.        
  19.         std::set<int> a;
  20.         std::set<int> b;
  21.         std::set<int> c1;
  22.         std::set<int> c2;
  23.         std::set<int> d;
  24.        
  25.         std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(a, a.begin())); // 交集
  26.         std::set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(b, b.begin())); // 合集
  27.         std::set_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(c1, c1.begin())); // 差
  28.         std::set_difference(y.begin(), y.end(), x.begin(), x.end(), inserter(c2, c2.begin())); // 差
  29.         std::set_symmetric_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(d, d.begin())); // 对称差
  30.        
  31.         print("x和y的交集", a);
  32.         print("x和y的合集", b);
  33.         print("x差y的集", c1);
  34.         print("y差x的集", c2);
  35.         print("x和y的对称差集", d);
  36.        
  37.         return 0;
  38. }
复制代码
  1. x集: 3 5 6 12
  2. y集: 1 5 6 7 10
  3. x和y的交集: 5 6
  4. x和y的合集: 1 3 5 6 7 10 12
  5. x差y的集: 3 12
  6. y差x的集: 1 7 10
  7. x和y的对称差集: 1 3 7 10 12
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-14 12:46:04 From FishC Mobile | 显示全部楼层
数字存进数组,然后循环判断。不知道C库有没有专门的函数,没有的话就自己写吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 13:07:55 From FishC Mobile | 显示全部楼层
C 没有,要自己写。C++ 有 set 库
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 13:19:19 | 显示全部楼层
zzxhh628 发表于 2022-4-14 12:46
数字存进数组,然后循环判断。不知道C库有没有专门的函数,没有的话就自己写吧。

能详细的说一下嘛?或者能直接告诉我代码吗?麻烦你了  up主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 13:20:03 | 显示全部楼层
傻眼貓咪 发表于 2022-4-14 13:07
C 没有,要自己写。C++ 有 set 库

能详细的说一下嘛?或者能直接告诉我代码吗?麻烦你了  up主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 13:26:53 From FishC Mobile | 显示全部楼层
你可以写一个函数,参数是两个数组,就是用for循环和if语句判断符不符合∩一类的条件,符合的然后存到另一个数组里,有必要进行顺序排序,然后把结果传出去就行了。具体代码网上有很多,我有时间再写吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 13:28:55 | 显示全部楼层
zzxhh628 发表于 2022-4-14 13:26
你可以写一个函数,参数是两个数组,就是用for循环和if语句判断符不符合∩一类的条件,符合的然后存到另一 ...

好的,如果你有时间能写一下就完美了 ,谢谢你了~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 13:31:59 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
执迷不悟0527 发表于 2022-4-14 13:28
好的,如果你有时间能写一下就完美了 ,谢谢你了~


C++
  1. #include <iostream>
  2. #include <set>

  3. void print(std::string str, std::set<int> set){
  4.         std::cout << str << ": ";
  5.         for(const int& n: set){
  6.                 std::cout
  7.                         << n
  8.                         << " ";
  9.         }
  10.         std::cout << std::endl;
  11. }

  12. int main(){
  13.         std::set<int> x = {3, 12, 5, 6};
  14.         std::set<int> y = {6, 10, 5, 1, 7};
  15.        
  16.         print("x集", x);
  17.         print("y集", y);
  18.        
  19.         std::set<int> a;
  20.         std::set<int> b;
  21.         std::set<int> c1;
  22.         std::set<int> c2;
  23.         std::set<int> d;
  24.        
  25.         std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(a, a.begin())); // 交集
  26.         std::set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(b, b.begin())); // 合集
  27.         std::set_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(c1, c1.begin())); // 差
  28.         std::set_difference(y.begin(), y.end(), x.begin(), x.end(), inserter(c2, c2.begin())); // 差
  29.         std::set_symmetric_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(d, d.begin())); // 对称差
  30.        
  31.         print("x和y的交集", a);
  32.         print("x和y的合集", b);
  33.         print("x差y的集", c1);
  34.         print("y差x的集", c2);
  35.         print("x和y的对称差集", d);
  36.        
  37.         return 0;
  38. }
复制代码
  1. x集: 3 5 6 12
  2. y集: 1 5 6 7 10
  3. x和y的交集: 5 6
  4. x和y的合集: 1 3 5 6 7 10 12
  5. x差y的集: 3 12
  6. y差x的集: 1 7 10
  7. x和y的对称差集: 1 3 7 10 12
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 15:02:25 | 显示全部楼层

你好博主,我把您的代码放在devc++里面为什么显示运行错误呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 15:10:13 From FishC Mobile | 显示全部楼层
文件后缀 .cpp
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-15 09:49:50 | 显示全部楼层

是cpp的后缀为什么还是不对呢???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-15 10:11:07 | 显示全部楼层
执迷不悟0527 发表于 2022-4-15 09:49
是cpp的后缀为什么还是不对呢???

请问报错什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-15 10:13:27 | 显示全部楼层
执迷不悟0527 发表于 2022-4-15 09:49
是cpp的后缀为什么还是不对呢???


试试多加这两行:
  1. #include <algorithm>
  2. #include <iterator>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-15 10:24:49 | 显示全部楼层

[Error] range-based 'for' loops are not allowed in C++98 mode
In function 'int main()':
[Error] in C+ +98 'x' must be initialized by constructor, not by "(..…"
[Error] could not convert '(3, 12, 5, 6' from '<brace-enclosed initializer list>' to 'std:set<int> [Error] in C++98'y' must be initialized by constructor, not by '(...”
[Error] could not convert '(6,10, 5, 1, 7Y from'<brace-enclosed initializer list>' to 'std:setsint>
这是全部报错了~~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-15 10:31:15 | 显示全部楼层
执迷不悟0527 发表于 2022-4-15 10:24
[Error] range-based 'for' loops are not allowed in C++98 mode
In function 'int main()':
[Error]  ...


版本太旧了,C++ 98 (1998 年)基本没有人再用了,试试更新吧,我用的是 C++20
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-18 15:25:48 | 显示全部楼层
在这里插个眼,暂时加不了好友,怕一会找不到大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 19:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表