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

問題は、if、while、do、for、switch、conditional expression(?:)、および equals などの他の一般的な組み込みメソッドを使用せずに、同じ o/p を実現することです。

ロジックとコードを教えてください。

4

6 に答える 6

4

以下は、かなり単純なオプションの 1 つです。

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);
}

マップを持たない言語で switch ステートメントをエミュレートするためにマップを使用することは、かなり一般的です。それらを使用して if ステートメントをエミュレートすることは、おそらく悪用です。

于 2013-05-19T02:26:05.460 に答える
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. 少しいじる... @Vladの答えのように


とにかく、面接の質問のポイントは答えではなく、何かに到達するために既成概念にとらわれずに考えることができるかどうかです. 最も実用的な答えは、「要件を変更する...これは正気ではありません」です。

于 2013-05-19T02:31:44.320 に答える
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);
}
于 2013-05-19T02:39:42.510 に答える
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);
}
于 2013-05-19T03:46:28.760 に答える
0

いいえif, while, do, for, switch, inline if (?:), またはその他の演算子 ( ==, !=, >, <,>=など):

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

ロジック:aと の両方をcに追加しSetます。それらが等しい場合、それらは 1 回だけ追加され、セットのサイズは になります1。それらが異なる場合、サイズは になります2

于 2013-05-19T05:41:21.527 に答える