PTA6-5 使用函数求1到10的阶乘和(C)

本题要求实现一个计算非负整数阶乘的简单函数,使得可以利用该函数,计算1!+2!+⋯+10!的值。

函数接口定义:

double fact( int n );

其中n是用户传入的参数,其值不超过10。如果n是非负整数,则该函数必须返回n的阶乘。

裁判测试程序样例:

#include <stdio.h>

double fact( int n );

int main(void)

{

int i;

double sum;

复制代码
sum = 0; 
for(i = 1; i <= 10; i++) 
    sum = sum + fact(i); 
    
printf("1!+2!+...+10! = %f\n", sum); 
return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:

本题没有输入。

输出样例:

1!+2!+...+10! = 4037913.000000

c 复制代码
double fact( int n ){
    if(n <= 1)
        return 1;
    return n*fact(n-1);
}
相关推荐
孤飞1 天前
zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线
算法
技术专家1 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
dllxhcjla1 天前
微服务全套
java
csdn_aspnet1 天前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
亚历克斯神1 天前
JVM 内存管理 2026:深度解析与调优实战
java·spring·微服务
鹿角片ljp1 天前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
逻辑驱动的ken1 天前
Java高频面试题:03
java·开发语言·面试·求职招聘·春招
噜噜大王_1 天前
深入理解 C 语言内存操作函数:memcpy、memmove、memset、memcmp
c语言·开发语言
广师大-Wzx1 天前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql
野生技术架构师1 天前
Java NIO到底是个什么东西?
java·开发语言·nio