LINTCODE——阶乘除法的最后一位数
题目:给出两个数 A 和 B, 其中 B >= A. 我们需要计算结果 F 的最后一位数是什么, 其中F = B! / A!(1 <= A, B <= 10^18, A 和 B 非常大)
思路:每次取最后一位数相乘之后再取最后一位数迭代就好了
class Solution {
public:
/*
* @param : the given number
* @param : another number
* @return: the last digit of B! / A!
*/
int computeLastDigit(long long A, long long B) {
// write your code here
int ans = 1;
for(auto i = A+1; i <= B; i++) {
ans = ((i%10)*ans)%10;
if (ans == 0)
return ans;
}
return ans;
}
};