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 を実現することです。
ロジックとコードを教えてください。
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 を実現することです。
ロジックとコードを教えてください。
以下は、かなり単純なオプションの 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 ステートメントをエミュレートすることは、おそらく悪用です。
次のようないくつかの可能なアプローチがあります。
ネイティブ コードでテストを実行します。それはずるい。
ジョブを実行するために使用できるライブラリ クラスを見つけます。このアプローチにはおそらく多くのバリエーションがあります。たとえば、@Cairnarvonの回答を参照してください。
入力に応じて例外を生成する (または生成しない) ために、何かトリッキーなことを行います。私の最初のアイデアは、ゼロ除算を使用することでしたが、別の方法があります...
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;
}
少しいじる... @Vladの答えのように
とにかく、面接の質問のポイントは答えではなく、何かに到達するために既成概念にとらわれずに考えることができるかどうかです. 最も実用的な答えは、「要件を変更する...これは正気ではありません」です。
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);
}
別の方法
ベースアイデア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);
}
いいえ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
。