2 zswyuzhoufeng zswyuzhoufeng 于 2014.08.05 21:58 提问

java面试题金山实习生

.输入4个数,构成两个区间,输出他们的整数交集区间。例如:1,10 , 5 , 15.输出【5,10】;写出思路和代码

1个回答

zhouhua0104
zhouhua0104   2014.08.06 17:17
/**
 * 前提 a<=b c<=d
 * 
 * @param a
 * @param b
 * @param c
 * @param d
 * @return
 */
public static String getResult(int a, int b, int c, int d) {
    String result = "";
    if (b < c) {
        result = "";
    }
    if (a > d) {
        result = "";
    }
    if (a < c && b > c && b < d) {
        result = "[" + c + "," + b + "]";
    }
    if (a > c && b > d) {
        result = "[" + a + "," + d + "]";
    }
    if (a > c && b < d) {
        result = "[" + a + "," + b + "]";
    }

    if (a < c && b > d) {
        result = "[" + c + "," + d + "]";
    }

    return result;

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!