急需设计一个C++程序 输入两个集合 输出交集并集和差集

如输入两个闭区间[1,10]、[5,20],输出交集[5,10],并集[1,20],差集[1,4]... 如输入两个闭区间[1,10]、[5,20],输出交集[5,10],并集[1,20],差集[1,4] 展开
 我来答
匿名用户
2013-07-13
展开全部
#include<iostream>
using namespace std;

int main()
{
int a,b;
int c,d;
cout<<"请输入第一个闭区间的a,b"<<endl;
cin>>a>>b;
cout<<"请输入第二个闭区间的c,d"<<endl;
cin>>c>>d;
if(a>b||c>d) {cout<<"输入的区间不合法"<<endl;}
else
{
if(d<a)
{
cout<<"交集为:空集"<<endl;
cout<<"并集为:"<<"["<<c<<","<<d<<"]"<<","<<"["<<a<<","<<b<<"]"<<endl;
cout<<"差集为:"<<"["<<a<<","<<b<<"]"<<endl;
}
else if(c>b)
{
cout<<"交集为:空集"<<endl;
cout<<"并集为:"<<"["<<a<<","<<b<<"]"<<","<<"["<<c<<","<<d<<"]"<<endl;
cout<<"差集为:"<<"["<<a<<","<<b<<"]"<<endl;
}
else if(c<a)
{
int min,max;
if(d>b) min=b,max=d;else min=d,max=b;
cout<<"交集为:"<<"["<<a<<","<<min<<"]"<<endl;
cout<<"并集为:"<<"["<<c<<","<<max<<"]"<<endl;
if(d>=b) cout<<"差集为:空集"<<endl;
else cout<<"差集为:"<<"["<<d-1<<","<<b<<"]"<<endl;
}
else
{
int min,max;
if(d>b) min=b,max=d;else min=d,max=b;
cout<<"交集为:"<<"["<<c<<","<<min<<"]"<<endl;
cout<<"并集为:"<<"["<<a<<","<<max<<"]"<<endl;
if(c<=a) cout<<"差集为:空集"<<endl;
else
{
cout<<"差集为:"<<"["<<a<<","<<c-1<<"]";
if(d<b)
{
cout<<","<<"["<<d-1<<","<<b<<"]";
}
cout<<endl;
}
}
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式