Cod sursa(job #2493418)

Utilizator iliescualexiaIliescu Alexia iliescualexia Data 16 noiembrie 2019 12:08:32
Problema Dezastru Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("dezastru.in");
ofstream fout("dezastru.out");
int st[30],n,k,i,nrs;
double v[30],p;
int validare(int niv)
{
    int i,ok=1;
    for(i=1;i<niv&&ok==1;i++)
    {
        if(st[i]==st[niv])
        {
            ok=0;
        }
    }
    return ok;
}
void prod(int niv,double &p)
{
    double x=1;
    int i;
    for(i=1;i<=niv;i++)
    {
        x=(double)(x*v[st[i]]);
    }
    p=p+x;
}
void bck(int niv,double &p)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        st[niv]=i;
        if(validare(niv)==1)
        {
            if(niv==k)
            {
                prod(niv,p);
                nrs++;
            }
            else
            {
                bck(niv+1,p);
            }
        }

    }
}
int main()
{
    fin>>n>>k;
    for(i=1;i<=n;i++)
    {
        fin>>v[i];
    }
    bck(1,p);
    p=p/nrs;
    fout<<fixed<<setprecision(6)<<p;
    return 0;
}