用不同方法实现求1!+2!+3!+…+20! 循环算法求1!+2!+3!+...+20! 递归算法求1!+2!+3!+...+20!
- public class Text {
- public static void main(String args[]) {
- Text t = new Text();
- System.out.print(t.sum());
- }
- public int sum() {
- int a = 0;
- for (int i = 1; i <= 10; i++) {
- int b = 1;
- for (int j = 1; j <= i; j++) {
- bb = b * j;
- }
- aa = a + b;
- }
- return a;
- }
- }
- public class Text {
- public static long fac(int n) {
- long res = 1;
- for (int k = 1; k <= n; k++)
- res *= k;
- return res;
- }
- public static void main(String args[]) throws IOException {
- long sum = 0;
- int i = 1, n = 20;
- for (i = 1; i <= n; i++)
- sum += fac(i);
- System.out.println("前" + n + "项阶乘的为:" + sum);
- }
- }
下面这个是递归吗?? 和上面貌似一模一样的啊!!!