6-6 求单链表结点的阶乘和

本文介绍了一个用于计算单链表中各节点值的阶乘和的函数设计方法。该函数遍历链表,对每个节点的数据计算阶乘,并累加得到最终结果。
部署运行你感兴趣的模型镜像

本题要求实现一个函数,求单链表L结点的阶乘和。这里默认所有结点的值非负,且题目保证结果在int范围内。

函数接口定义:

int FactorialSum( List L );

其中单链表List的定义如下:

typedef struct Node *PtrToNode;
struct Node {
    int Data; /* 存储结点数据 */
    PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node *PtrToNode;
struct Node {
    int Data; /* 存储结点数据 */
    PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

int FactorialSum( List L );

int main()
{
    int N, i;
    List L, p;

    scanf("%d", &N);
    L = NULL;
    for ( i=0; i<N; i++ ) {
        p = (List)malloc(sizeof(struct Node));
        scanf("%d", &p->Data);
        p->Next = L;  L = p;
    }
    printf("%d\n", FactorialSum(L));

    return 0;
}

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

输入样例:

3
5 3 6

输出样例:

846

int FactorialSum(List L)  
{  
    int i, Num;  
    int Sum = 0;  
    while (L != NULL)  //遍历整个链表 
    {  
        Num = 1;  
        for (i = 1; i <= L->Data; i++)//从每个节点读取数据计  
        {  
            Num = Num*i;  //算每个节点数据的阶乘 
        }  
        Sum = Sum + Num;  //对节点数据求和 
        L = L->Next;  
    }  
    return Sum;  //返回单链表结点的阶乘和
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### PTA 单链表节点阶乘 C/C++/Python 实现 #### 使用 C++ 计算单链表节点值的阶乘 为了实现这一功能,在C++中可以定义一个结构体`Node`表示链表中的结点,其中包含数据成员`data`存储该结点的数据以及指针成员`next`指向下一个结点。接着创建函数`factorialSum`遍历整个列表并累加各结点数据对应的阶乘。 ```cpp #include <iostream> using namespace std; struct Node { int data; struct Node* next; }; // Function to calculate factorial of a number n. int fact(int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } // Function that returns sum of factorials present at each node in the linked list pointed by head. long long factorialSum(struct Node* head) { long long sum = 0; while (head != NULL) { sum += fact(head->data); // Add up all the factorials from nodes into variable 'sum' head = head->next; // Move on to check next element/node within this Linked List structure } return sum; } ``` 上述代码实现了计算单链表内所有元素对应阶乘的功能[^3]。 #### Python 版本实现相同逻辑 对于Python而言,则不需要显式声明类型,因此可以直接利用类(Class)`ListNode`模拟链表行为,并通过迭代器访问各个位置上的对象实例完成同样的任务: ```python class ListNode: def __init__(self, val=0, nxt=None): self.val = val self.next = nxt def get_factorial(num): result = 1 for i in range(2, num + 1): result *= i return result def factorial_sum(head): total = 0 current_node = head while current_node is not None: total += get_factorial(current_node.val) current_node = current_node.next return total ``` 这段脚本同样完成了对输入链表中每一个整数值取其阶乘并将它们相加以获得最终的结果。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值