如何在不使用模数(%)运算符的情况下获取数字的最后一位?
How to get the last digit of a number without using modulus(%) operator?
If we are told that we can't use modulus operator then how can we take
out the last digit of a number.
例如
N=2345,
我们应该得到 5。
尽量提供通用的解决方案。
我发现了什么:
N- N/ 10 * 10
您提供的公式将起作用。
一般来说,对于 >= 0 的整数,这将始终为真
A % B = A - [A/B] * B,其中 [x] 表示最大整数 <= x
通过铸造。
但这之前需要进行一些检查。
简单示例:
int num = 15;
double d = num/10; //d = 1.5
num = num/10; //num = 1;
int lastNumber = (d - (double)num) * 10;
number = 2345;
arr = (""+number).split('');
console.log(arr[arr.length-1])
If we are told that we can't use modulus operator then how can we take out the last digit of a number.
例如
N=2345,
我们应该得到 5。
尽量提供通用的解决方案。
我发现了什么:
N- N/ 10 * 10
您提供的公式将起作用。
一般来说,对于 >= 0 的整数,这将始终为真
A % B = A - [A/B] * B,其中 [x] 表示最大整数 <= x
通过铸造。 但这之前需要进行一些检查。
简单示例:
int num = 15;
double d = num/10; //d = 1.5
num = num/10; //num = 1;
int lastNumber = (d - (double)num) * 10;
number = 2345;
arr = (""+number).split('');
console.log(arr[arr.length-1])