如何在不使用条件的情况下创建 return 1 或 0 的方法?
How to create a method to return 1 or 0 without using conditions?
我在接受 return 1 if provided 0 和 return 0 if provided 1 的采访时被问到一个问题,但没有使用条件,即 if、三元等
只是为了给你和下面没有 if 的代码的想法:
public int testMethod(int value){
if(value==0) return 1;
if(value==1) return 0;
return 0;
}
更新:
虽然@Usagi 的答案似乎最适合我编写的代码..但重新考虑我重新分析答案的问题..@Sergio 的答案似乎是最简单和最合适的..
public int testMethod(int value) {
return 1 - (value % 2); // or 1 - (value & 1)
}
这可以用来在任何值和 0 之间切换,EG 3:
public int testMethod3(int value) {
return 3 - (value % 4);
}
并且只是为了覆盖问题示例末尾的 return 0
:
private static final int[] VALUES = { 1, 0 };
public int testMethod(int value) {
try {
return VALUES[value];
} catch (ArrayIndexOutOfBoundsException ex) {
return 0;
}
}
假设您的语言有等同于 get the absolute value of this number
的东西,那么类似于:
public int testMethod(int value) {
return Math.abs(value - 1);
}
会起作用。
您可以像这样使用按位运算符:
value ^ 1
^ 是 "copies the bit if it is set in one operand but not both" 的按位异或运算符。 1和0在位中的表示如下:
1 = 0000 0001
0 = 0000 0000
所以当值 = 1 时,你最终会做:
1 ^ 1 = (0000 0001) ^ (0000 0001) = 0000 0000 = 0 因为它们共享相同的位 none 位被复制。
现在,如果值 = 0,你最终会做:
0 ^ 1 = (0000 0000) ^ (0000 0001) = 0000 0001 = 1 因为最后一位在一个操作数中为 1 而在另一个操作数中为 0。
我们可以在这里使用异或运算符。当有两个或零个 1 时,Xor 是 "exclusive or" 和 returns 0,如果恰好有一个 1,则为 returns 1。它对整数的每一位执行此操作。
所以比如二进制1001^1000 = 0001因为第一位有两个1所以是0,后面两个没有1所以是零,最后一位只有一个1,输出一个1。
public int testMethod(int value){
return value ^ 1;
}
我原来的回答
public int TestMethod(int value)
{
return Convert.ToInt32(!Convert.ToBoolean(value));
}
建议的修改后的
public int TestMethod(int value)
{
return Convert.ToInt32(value == 0);
}
另一种方法是基于 C#
中整数除法的行为并避免使用异常处理。
public int TestMethod(int value)
{
return 1 / ((10 * value) + 1);
}
所有三种方法都会return相同的结果:
In | Out
-2 | 0
-1 | 0
0 | 1
1 | 0
2 | 0
或者,try/catch 函数 除 0/值。
- 函数在不使用任何数学库的情况下工作;
- 函数适用于所有整数值;
public int MethodTest(int value)
{
try
{
return (0/value);
}
catch(Exception ex)
{
return 1;
}
}
值的选择是通过触发编译错误来完成的:
零除以零通常会引发编译错误。然后returns1;
A 零除以任何不同于零的值 returns 0;
字符串技巧!
Java:
public int testMethod(int value) {
return String.valueOf(value).substring(0, 1).indexOf('0') + 1;
}
C#:
public int testMethod(int value) {
return value.ToString().Substring(0, 1).IndexOf('0') + 1;
}
这依赖于 indexOf/IndexOf 如果未找到匹配则返回 -1。
Math.floor(1 / (1 + Math.abs(x)))
如果只给出 0 和 1 那么这可能更简单:
return 1 - value;
我认为问题是关于计算第 1 位的计数。
public int testMethod(int value){
// v--- count = value == 0 ? 32 : [0,32)
return Integer.bitCount(~value) / 32;
}
所以输出应该如下所示:
// v--- return 1
assert testMethod(0) == 1;
// v--- return 0
assert testMethod(nonZero) == 0;
考虑到输入只是 [1, 0]
也可以使方法成为 return 输入的 0 次方
在java
public int test(int value){
return Math.pow(0,value);
}
同样的逻辑也适用于任何其他语言
如果不允许其他输入
static int Test(int @value)
{
return (@value + 1) % 2;
}
使用按位异或可能是计算效率最高的方法
return value ^ 1
给定值 i 的范围是 [0, 1]:
public int test(int i) {
return !i;
}
这毫无意义...
请查看我的 C#
解决方案 (.NET Fiddle):
private static int Calculate(int x)
{
return ((-x ^ x) >> 31) + 1;
}
示例:
Input: 0; Output: 1;
Input: 1; Output: 0;
Input: 64; Output: 0;
Input: 65; Output: 0;
Input: -100; Output: 0;
Input: -101; Output: 0;
Input: 102; Output: 0;
Input: 888887; Output: 0;
Input: 2147483647; Output: 0;
Input: -2147483648; Output: 1;
它适用于 所有 int
值(int.MinValue
除外)。
仅使用了逻辑和算术运算,没有使用 Math
、Convert
等 类。
解释:
- 对输入数
x
和负数输入数 -1 * x
执行 XOR 运算符。 C#
的异或运算符是 (-x ^ x)
- XOR 运算符 returns 带符号位的数字如果
x
是非零数字(当然 XOR 与零数字 returns 0)
- 符号位是数字的左位。符号位是
int
数字的第 32 位。
- 执行right-shift运算符并将符号位放在
int
数字的第一位:(-x ^ x) >> 31
(-x ^ x) >> 31
returns -1 对于任何非零 int
值(对于零数它 returns 0)
- 加 1 和 return 结果
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/x4HCYj" frameborder="0"></iframe>
您正在寻找这样的东西
var status = $_POST['status'];
status=='1'?'0':'1';
数字(!值)
它会 return 真值或假值并将其转换回数字,您将从 1 得到 0,从 0 得到 1。
效率不高,但很有趣:
public int neg(int n){
return (int) Math.pow(0, n);
}
我在接受 return 1 if provided 0 和 return 0 if provided 1 的采访时被问到一个问题,但没有使用条件,即 if、三元等
只是为了给你和下面没有 if 的代码的想法:
public int testMethod(int value){
if(value==0) return 1;
if(value==1) return 0;
return 0;
}
更新: 虽然@Usagi 的答案似乎最适合我编写的代码..但重新考虑我重新分析答案的问题..@Sergio 的答案似乎是最简单和最合适的..
public int testMethod(int value) {
return 1 - (value % 2); // or 1 - (value & 1)
}
这可以用来在任何值和 0 之间切换,EG 3:
public int testMethod3(int value) {
return 3 - (value % 4);
}
并且只是为了覆盖问题示例末尾的 return 0
:
private static final int[] VALUES = { 1, 0 };
public int testMethod(int value) {
try {
return VALUES[value];
} catch (ArrayIndexOutOfBoundsException ex) {
return 0;
}
}
假设您的语言有等同于 get the absolute value of this number
的东西,那么类似于:
public int testMethod(int value) {
return Math.abs(value - 1);
}
会起作用。
您可以像这样使用按位运算符:
value ^ 1
^ 是 "copies the bit if it is set in one operand but not both" 的按位异或运算符。 1和0在位中的表示如下:
1 = 0000 0001
0 = 0000 0000
所以当值 = 1 时,你最终会做:
1 ^ 1 = (0000 0001) ^ (0000 0001) = 0000 0000 = 0 因为它们共享相同的位 none 位被复制。
现在,如果值 = 0,你最终会做:
0 ^ 1 = (0000 0000) ^ (0000 0001) = 0000 0001 = 1 因为最后一位在一个操作数中为 1 而在另一个操作数中为 0。
我们可以在这里使用异或运算符。当有两个或零个 1 时,Xor 是 "exclusive or" 和 returns 0,如果恰好有一个 1,则为 returns 1。它对整数的每一位执行此操作。
所以比如二进制1001^1000 = 0001因为第一位有两个1所以是0,后面两个没有1所以是零,最后一位只有一个1,输出一个1。
public int testMethod(int value){
return value ^ 1;
}
我原来的回答
public int TestMethod(int value)
{
return Convert.ToInt32(!Convert.ToBoolean(value));
}
建议的修改后的
public int TestMethod(int value)
{
return Convert.ToInt32(value == 0);
}
另一种方法是基于 C#
中整数除法的行为并避免使用异常处理。
public int TestMethod(int value)
{
return 1 / ((10 * value) + 1);
}
所有三种方法都会return相同的结果:
In | Out
-2 | 0
-1 | 0
0 | 1
1 | 0
2 | 0
或者,try/catch 函数 除 0/值。
- 函数在不使用任何数学库的情况下工作;
- 函数适用于所有整数值;
public int MethodTest(int value)
{
try
{
return (0/value);
}
catch(Exception ex)
{
return 1;
}
}
值的选择是通过触发编译错误来完成的:
零除以零通常会引发编译错误。然后returns1;
A 零除以任何不同于零的值 returns 0;
字符串技巧!
Java:
public int testMethod(int value) {
return String.valueOf(value).substring(0, 1).indexOf('0') + 1;
}
C#:
public int testMethod(int value) {
return value.ToString().Substring(0, 1).IndexOf('0') + 1;
}
这依赖于 indexOf/IndexOf 如果未找到匹配则返回 -1。
Math.floor(1 / (1 + Math.abs(x)))
如果只给出 0 和 1 那么这可能更简单:
return 1 - value;
我认为问题是关于计算第 1 位的计数。
public int testMethod(int value){
// v--- count = value == 0 ? 32 : [0,32)
return Integer.bitCount(~value) / 32;
}
所以输出应该如下所示:
// v--- return 1
assert testMethod(0) == 1;
// v--- return 0
assert testMethod(nonZero) == 0;
考虑到输入只是 [1, 0]
也可以使方法成为 return 输入的 0 次方
在java
public int test(int value){
return Math.pow(0,value);
}
同样的逻辑也适用于任何其他语言
如果不允许其他输入
static int Test(int @value)
{
return (@value + 1) % 2;
}
使用按位异或可能是计算效率最高的方法
return value ^ 1
给定值 i 的范围是 [0, 1]:
public int test(int i) {
return !i;
}
这毫无意义...
请查看我的 C#
解决方案 (.NET Fiddle):
private static int Calculate(int x)
{
return ((-x ^ x) >> 31) + 1;
}
示例:
Input: 0; Output: 1;
Input: 1; Output: 0;
Input: 64; Output: 0;
Input: 65; Output: 0;
Input: -100; Output: 0;
Input: -101; Output: 0;
Input: 102; Output: 0;
Input: 888887; Output: 0;
Input: 2147483647; Output: 0;
Input: -2147483648; Output: 1;
它适用于 所有 int
值(int.MinValue
除外)。
仅使用了逻辑和算术运算,没有使用 Math
、Convert
等 类。
解释:
- 对输入数
x
和负数输入数-1 * x
执行 XOR 运算符。C#
的异或运算符是(-x ^ x)
- XOR 运算符 returns 带符号位的数字如果
x
是非零数字(当然 XOR 与零数字 returns 0) - 符号位是数字的左位。符号位是
int
数字的第 32 位。 - 执行right-shift运算符并将符号位放在
int
数字的第一位:(-x ^ x) >> 31
(-x ^ x) >> 31
returns -1 对于任何非零int
值(对于零数它 returns 0)- 加 1 和 return 结果
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/x4HCYj" frameborder="0"></iframe>
您正在寻找这样的东西
var status = $_POST['status'];
status=='1'?'0':'1';
数字(!值)
它会 return 真值或假值并将其转换回数字,您将从 1 得到 0,从 0 得到 1。
效率不高,但很有趣:
public int neg(int n){
return (int) Math.pow(0, n);
}