c语言用递归求:10的阶乘、100+99+98...+3+2+1的值。

#include <stdio.h>
#include <string.h>
void main(){
    int myfun(int in);
    int  a =myfun(10);
    printf("%d\n", a);
}

int myfun(int in){
    if (in > 1)
        //将下面的*换成+,就可以求100+99+98...+3+2+1的值呢。
        return in * myfun(in-1);
    else
        return 1;
}

原文地址:https://www.cnblogs.com/airduce/p/8686127.html

时间: 2024-07-30 02:09:59

c语言用递归求:10的阶乘、100+99+98...+3+2+1的值。的相关文章

C语言用递归求斐波那契数,让你发现递归的缺陷和效率瓶颈

递归是一种强有力的技巧,但和其他技巧一样,它也可能被误用. 一般需要递归解决的问题有两个特点: 存在限制条件,当符合这个条件时递归便不再继续: 每次递归调用之后越来越接近这个限制条件. 递归使用最常见的一个例子就是求阶乘,具体描述和代码请看这里:C语言递归和迭代法求阶乘 但是,递归函数调用将涉及一些运行时开销--参数必须压到堆栈中,为局部变量分配内存空间(所有递归均如此,并非特指求阶乘这个例子),寄存器的值必须保存等.当递归函数的每次调用返回时,上述这些操作必须还原,恢复成原来的样子.所以, 基

递归--求n!的阶乘结果

递归的基本概念? 一个函数调用其自身,就是递归? 求n!的递归函数 通过函数递归的方式实现求函数的阶乘. Python代码如下: #求阶乘 def Factorial(n): if (n == 1): return 1 else: return n * Factorial(n-1) def main(): n = int(input("请输入需要计算的阶乘数值:")) rtn = Factorial(n) print("数值%d阶乘的计算结果为:%d" %(n,rt

C语言习题【4】递归和非递归求n的阶乘

#include<stdio.h> #include<math.h> int main() { int n; int sum = 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { sum *= i; } printf("%d", sum); return 0; } //非递归 #include<stdio.h> int floor(int n) { if (n ==

【C语言】 递归求非负数的每一位之和

#include<stdio.h> int DigitSum(unsigned int n)    { int num = 0; if( n == 0) {     return num; } else num = n%10; n /= 10; return num + DigitSum(n); } int main() {      unsigned int n = 0; int ret = 0; printf("请输入一个非负整数:"); scanf("%d&

Java 1:利用递归、非递归求n的阶乘

我们都知道n!=123.......(n-1)n;并且当n=0或n=1时n!=1,所以从公式可以将计算条件分为2部分:1.(n==0||n==1) ,n!=12.(n>1) ,n!=(n-1)!*n代码实现如下: public class Practice0514{ public static long factorial1(int n){//递归计算n的阶乘 if(n==0||n==1){ return 1; } else{ return factorial1(n-1)*n; } } publ

9.求10!

要求:求10的阶乘 #include <stdio.h> int main(int argc, char *argv[]) { float a = 10,i = 2,j = 1;//用float避免溢出 while(i<=a) { j = j*i; i++; } printf("10! = %f",j); return 0; } 原文地址:https://www.cnblogs.com/Aidongshu/p/8305895.html

利用递归求阶乘

1 package com.d; 2 3 import java.util.Scanner; 4 5 public class Digui { 6 7 public static void main(String[] args) { 8 Digui d = new Digui(); 9 10 System.out.println("请输入一个整数"); 11 Scanner scan = new Scanner(System.in); 12 int a = scan.nextInt()

递归学习(一)最简单的C语言递归求年龄算法

递归是我们在学习编程中,必须要去学习的,虽然递归晦涩难懂 ,但是很多时候,递归的思想会很有用,但是在实际开发中,不建议使用递归,要用循环来代替递归,不然bug无穷. ----------------------------------------------------------- 问题描述: 有5个人坐在一起, 问第5个人,他说比第4个人大2岁, 问第4个人,他说比第3个人大2岁, 问第3个人,他说比第2个人大2岁, 问第2个人,他说比第1个人大2岁, 问最后一个人,他说10岁 第5个人多大

[Lua学习]递归求阶乘

1 --递归求阶乘 2 function func(n) 3 if n ~= 1 then 4 return n * func(n - 1) 5 else 6 return 1 7 end 8 end 9 10 print("输入整数:") 11 a = io.read("*number") 12 print(a .. "! -> " .. func(a)) ps:函数内也可以用(a and b) or c 这种形式的语句替代(未亲测).