Cod sursa(job #2493330)

Utilizator dogaru_roxanaDogaru Roxana dogaru_roxana Data 16 noiembrie 2019 11:41:01
Problema Dezastru Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <iomanip>
using namespace std;

ifstream fin ("dezastru.in");
ofstream fout ("dezastru.out");

int n, k, nr, st[30];
double p[30], s;

int validare (int niv)
{
    int i;
    for (i=1; i<niv; i++)
    {
        if (st[i]==st[niv])
            return 0;
    }
    return 1;
}

void bck (int niv, double prod)
{
    int i, j;
    for (i=1; i<=n; i++)
    {
        st[niv]=i;
        if (validare(niv)==1)
        {
            if (niv==k)
            {
                s=s+prod*p[st[niv]];
                nr++;
            }
            else
                bck (niv+1, prod*p[st[niv]]);
        }
    }
}
int main()
{
    int i;
    fin>>n>>k;

    for (i=1; i<=n; i++)
        fin>>p[i];

    bck (1, 1);
    fout<<fixed<<setprecision(6)<<s/nr;

    fin.close();
    fout.close();
    return 0;
}