👤

Se dau n cifre . Calculati suma factorialelor acestor cifre.

Exemplu: n=3
4 2 1
=>27
(4!+2!+1!)
Multumesc!


Răspuns :

#include <iostream>

int main()
{
    const int totalFacts = 21;
    long long factorials[totalFacts] {};

    factorials[0] = 1;
    for (int i = 1; i < totalFacts; ++i)
        factorials[i] = factorials[i - 1] * i;

    int n;
    long long s = 0;
    std::cin >> n;
    for (int i = 0, x; i < n; ++i) {
        std::cin >> x;
        s += factorials[x];
    }

    std::cout << s;
}

#include <iostream>

using namespace std;

int n,x;
unsigned long long int S;

int main ()
{

    cin >> n;

    for(;n;n--)
    {
        cin >> x;
        unsigned long long int f=1;
        if(x>1) for(;x;x--) f*=x;
        S+=f;
    }

    cout << S;

    return 0;

}