include <stdio.h> long jc (int num); long jc2 (int num); int main() { long n; n = jc(5); printf("%d", n); // n = jc2(5); // printf("%d", n); } /** * for循环 */ long jc (int num) { long j = 1, i; for (i = 1; i <= num; i++){ j *= i; //一直乘以步长+1的数,直到自增到参数 } return j; } /**递归 * 5! = 1 * 2 * 3 * 4 * 5; * 4! = 1 * 2 * 3 * 4; * 3! = 1 * 2 * 3; * 2! = 1 * 2; * 1! = 1; **/ long jc2 (int num) { long j; //printf("%d", num); if (num > 1) { //printf("%d", num); j = jc2(num - 1) * num; //如果要求5的阶乘,那么先求出比5小1的阶乘以5。 } else { //printf("%d", num); j = 1; } return j; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17128.html