1. 求整数区间[a,b]和[c,d]的交集、并集
package cn.oop.nk;
/**
* 求整数区间[a,b]和[c,d]的交集、并集
* @author 温暖wk
*
*/
public class Demo02 {
public static void main(String[] args) {
getInterval(1,9,3,10);
getsum(1,9,3,10);
}
//求交集
public static void getInterval(int s1,int e1,int s2,int e2){
if(e1 < s2 || e2 < s1) {
System.out.println("两集合无交集");
}else{
System.out.println("两集合的交集为:[" + Math.max(s1,s2) + "," + Math.min(e1,e2) + "]");
}
}
//求并集
public static void getsum(int s1,int e1,int s2,int e2){
if(s1 < s2 || e2 > e1) {
System.out.println("两集合的并集为:[" + Math.min(s1,s2) + "," + Math.max(e1,e2) + "]");
}
}
}