Basic programming topic set 6-6 Find the sum of factorials of singly linked list nodes (15 points)

Problem Description:

Problem solving ideas:

Here I choose to use the search with memo (recursion) to obtain the factorial of a number, to avoid the easy timeout when the number of the test sample is relatively large, so there is no need to loop for the factorial

Problem solving code:

int a[100] = {1};
int f(int x){
    if(a[x]) return a[x];
    if(x == 1) return 1;
    else return a[x] = x * f(x-1);
}
int FactorialSum( List L ){
    int ans = 0;
    while(L != NULL){
        ans += f(L->Data);
        L = L->Next;
    }
    return ans;
}

Leave a Comment

Your email address will not be published. Required fields are marked *