递归算法编写函数,求n!,在主函数中调用这个函数,并输出如下结果:a. 1!+2!+3!+4!+…+10!b. 1!+3!+5!+7!c. 5!+11!+17!
时间: 2024-01-02 13:21:35 浏览: 96
以下是求n!以及三个结果的代码实现:
```java
public class Recursion {
public static void main(String[] args) {
int sum1 = 0, sum2 = 0, sum3 = 0;
for (int i = 1; i <= 10; i++) {
sum1 += Recur(i);
if (i % 2 == 1) {
sum2 += Recur(i);
}
if (i >= 5) {
sum3 += Recur(6 * i - 20);
}
}
System.out.println("1!+2!+3!+4!+…+10! = " + sum1);
System.out.println("1!+3!+5!+7! = " + sum2);
System.out.println("5!+11!+17! = " + sum3);
}
public static int Recur(int n) {
if (n == 1) {
return 1;
} else {
return n * Recur(n - 1);
}
}
}
```
阅读全文