这是 拼题A(PTA)《中M2019秋C入门和进阶练习集》的习题。原题在 https://pintia.cn/problem-sets/1163286449659043840/problems/1174288506294865924 (侵删)
本人的答案仅供交流学习,请勿用于当作答案来提交!
题目描述:
6-5 求单链表结点的阶乘和 (15 point(s))
本题要求实现一个函数,求单链表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
我的答案:
/*================================================================
* Copyright (C) 2019 程序知路. All rights reserved.
*
* Filename :6-5-求单链表结点的阶乘和.c
* Author :程序知路
* E-Mail :admin@icxzl.com
* Create Date :2019年09月26日
* Description :
================================================================*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Data; // 存储结点数据
PtrToNode Next; // 指向下一个结点的指针
};
typedef PtrToNode List; // 定义单链表类型
typedef struct Node *PtrToNode;
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;
}
// 以下是有效代码
int FactorialSum( List L ) {
int sum = 0;
int product = 1;
int count = 1;
while (L) {
product = 1;
count = 1;
while (count <= L->Data) {
product *= count ++;
}
sum += product;
L = L->Next;
}
return sum;
}
鉴于本人的相关知识储备以及能力有限,本博客的观点和描述如有错漏或是有考虑不周到的地方还请多多包涵,欢迎互相探讨,一起学习,共同进步。
本文章可以转载,但是需要说明来源出处!
本文使用的部分图片来源于网上,若是侵权,请与本文作者联系删除: admin@icxzl.com