C语言,选择结构。输入两个闭区间,求他们的交集。
例如:闭区间1,10和闭区间5,20,输出它们的交集:5,10闭区间1,10和闭区间15,20,输出它们的交集:空集闭区间1,10和闭区间-5,1,输出它们的交集:1,1...
例如:
闭区间1,10和闭区间5,20,输出它们的交集:5,10
闭区间1,10和闭区间15,20,输出它们的交集:空集
闭区间1,10和闭区间-5,1,输出它们的交集:1,1 展开
闭区间1,10和闭区间5,20,输出它们的交集:5,10
闭区间1,10和闭区间15,20,输出它们的交集:空集
闭区间1,10和闭区间-5,1,输出它们的交集:1,1 展开
2个回答
展开全部
#include <stdio.h>
int main()
{
int low1,up1,low2,up2;
printf("输入第一个闭区间:");
scanf("%d,%d",&low1,&up1);
printf("输入第二个闭区间:");
scanf("%d,%d",&low2,&up2);
printf("交集:");
if(low1>up2 || low2>up1)
printf("空集\n");
else if(up1>=up2)
{
if(low1>low2)
printf("[%d,%d]",low1,up2);
else
printf("[%d,%d]",low2,up2);
}
else if(up2>up1)
{
if(low2>low1)
printf("[%d,%d]",low2,up1);
else
printf("[%d,%d]",low1,up1);
}
}
2013-06-08
展开全部
#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;} 望采纳,谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询