Java递归求和1+2+3+...+n public class Sum { public static int count(int n) { if (n 1) { return count(n - 1) + n; } else { return n; } } public static void main(String[] args) { System.out.println(Sum.count(100)); }} 扩展学习 输入一个数: 4
Java递归求和1+2+3+...+n
思路:计算前n个数的总和等于第n-1个数+n; |
2021-06-05
2021-05-27
2021-05-26
2021-06-05
2021-05-16