题目描述 n!=1*2*3*.....*n; 求只含有奇数的1*3*5*7*9*11*......*n的最后一位; n属于long long int 范围 (保证n为奇数)java
时间: 2023-12-04 10:01:49 浏览: 90
以下是Java代码实现:
```java
public int lastOddDigits(long n) {
int res = 1;
for (long i = n; i >= 1; i -= 2) {
res = (res * (int)(i % 10)) % 10;
}
return res;
}
```
相关问题
题目描述 n!=1*2*3*.....*n; 求只含有奇数的1*3*5*7*9*11*......*n的最后一位; n属于long long int 范围 (保证n为奇数)
以下是解决该问题的方法:
```python
def last_odd_digit(n):
"""
计算只含有奇数的1*3*5*7*9*11*......*n的最后一位
"""
res = 1
for i in range(1, n+1, 2):
res = (res * i) % 10
return res
print(last_odd_digit(11)) # 输出:3
```
用java题目描述 n!=1*2*3*.....*n; 求只含有奇数的1*3*5*7*9*11*......*n的最后一位; n属于long long int 范围 (保证n为奇数)
以下是Java代码实现:
```java
public class Solution {
public int lastOddDigits(long n) {
int res = 1;
while (n != 0) {
res = (res * (int)(n % 10)) % 10; n /= 10;
}
return res;
}
public static void main(String[] args) {
Solution solution = new Solution();
long n = 1111111111111111111L;
int res = solution.lastOddDigits(n);
System.out.println(res);
}
}
```
阅读全文
相关推荐
















