如果等效而不使用条件运算和其他循环方法java

时间:2013-05-19 02:14:30

标签: java

int function(int a, int b, int c){
        if(a==c)
            return a;
        else 
            return b;
    }

问题是在不使用if,while,do,for,switch,条件表达式(?:)和其他一般内置方法(如equals

)的情况下实现相同的o / p

请告诉我逻辑和代码......

6 个答案:

答案 0 :(得分:4)

这是一个相当简单的选择:

int function(int a, int b, int c) {
    java.util.HashMap<Boolean, Integer> map = new java.util.HashMap<Boolean, Integer>();

    map.put(true, a);
    map.put(false, b);

    return map.get(a == c);
}

使用map来模拟没有它们的语言中的switch语句是很常见的。使用它们来模拟if语句可能是滥用。

答案 1 :(得分:3)

有许多可能的方法,包括:

  1. 在本机代码中进行测试。那是作弊。

  2. 找到一些可用于完成工作的库类。这种方法可能有很多变化;例如见@ Cairnarvon的答案。

  3. 根据输入做一些棘手的事情来生成异常(或不生成异常)。我最初的想法是使用除零,但这是另一种方式...

    int insanelyStupidConditional (int a, int b, int c) {
        int[] dummy = new int[1];
    
        try {
            int foo = dummy[a - c];
        } catch (ArrayIndexOutOfBoundsException ex) {
            return b;
        }
        return a;
    }
    
  4. 有点笨拙......就像@弗拉德的回答


  5. 无论如何,面试问题的重点不在于答案,而在于你是否能够能够在盒子外思考以达到某种目的。 最实用的答案是&#34;更改要求......这是疯狂的&#34;。

答案 2 :(得分:3)

我真的希望我想出了Cairnarvon的解决方案。这是我得到的,但无论如何你最终都会在函数调用中隐藏某个条件语句,除非你能弄清楚如何用位运算符来做这个。

public static int fn(int a, int b, int c) {
    Boolean equal = (a == c);

    //if equal is false, compareTo will return 0.
    //if equal is true, compareTo will return any positive integer, thus we take mod 2 to ensure this is 1
    int ret_a = equal.compareTo(Boolean.FALSE) % 2;

    //if ret_a is 0, make ret_b = 1
    //if ret_a is 1, make ret_b = 0
    int ret_b = (ret_a + 1) % 2;

    //one of these two terms is guaranteed to be zero, therefore you will only
    //return the value of a, or b.
    return (ret_a * a) + (ret_b * b);
}

这是我尝试的解决方案,没有比较或有点笨拙。可悲的是@Pshemo指出我的逻辑是有缺陷的。

    public static int fn(int a, int b, int c) {

    //I assumed this will return 1 if not a != c
            //See Pshemo's comment about why this is wrong.
    int not_equal = ((a - c) * (a - c) ) % 2;

    int ret_a = (not_equal + 1) % 2;
    int ret_b = not_equal;

    //one of these two terms is guaranteed to be zero, therefore you will only
    //return the value of a, or b.
    return (ret_a * a) + (ret_b * b);
}

答案 3 :(得分:3)

这是一种仅使用运算符的方法:

int function(int a, int b, int c) {
    //If a == c: result = 0x00000000
    //Else:      result = 0xFFFFFFFF
    int result = (a - c | c - a) >> 31;

    //If a == c: result = 0x00000000 & (a ^ b) = 0
    //Else:      result = 0xFFFFFFFF & (a ^ b) = a ^ b
    result &= a ^ b;

    //If a == c: result = 0       ^ a = a
    //Else:      result = (a ^ b) ^ a = b
    result ^= a;

    return result;
}

答案 4 :(得分:1)

另一种方式

基本构思return b * f(a,c) + a * (1 - f(a,c))其中

  • f(a,c) -> 1 a != c

  • f(a,c) -> 0 a == c

所以

  • a!=c我们将返回b*(1) + a*(0);
  • ,对于a==c,我们将返回b*(0) + a*(1);

public static int test(int a, int b, int c) {
    // (a - c) | (c - a) will give 
    // for a != b negative value
    // for a == c zero

    // to get sign of that value we need to get highest bit
    // so >>>31 will do the trick

    int signum = ((a - c) | (c - a)) >>> 31;
    //for a == c -> signum = 0 
    //for a != c -> signum = 1 (it indicates that (a - c) | (c - a) was negative) 

    return b * signum + a * (1 - signum);
}

答案 5 :(得分:0)

你去了,没有ifwhiledoforswitch,内联(?:)或任何其他运营商(==!=><>=等):

int function(int a, int b, int c){
    int[] result = {-1, a, b};
    return result[new TreeSet<Integer>(Arrays.asList(a, c)).size()];
}

逻辑:将ac添加到Set。如果它们相等,它们只会被添加一次,而且集合的大小将为1。如果它们不同,则大小为2