Cod sursa(job #2541827)

Utilizator Florinos123Gaina Florin Florinos123 Data 8 februarie 2020 22:50:23
Problema Dezastru Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <iomanip>

using namespace std;

ifstream f ("dezastru.in");
ofstream g ("dezastru.out");

int n, k, sol[27];
double v[27], ans;
long long nr;

void read ()
{
int i;
 f >> n >> k;
  for (i=1; i<=n; i++)
        f >> v[i];
}

bool ok (int poz)
{
    for (int i=1; i<poz; i++)
        if (sol[i] == sol[poz])
           return false;
    return true;
}

void aduna ()
{
 int i;
 double aux = 1;
   for (i=1; i<=k; i++)
   {
       aux *= v[sol[i]];
   }
  ans += aux;
}

void bkt (int poz)
{
    for (int i=1; i<=n; i++)
    {
        sol[poz] = i;
         if (ok(poz))
         {
             if (poz == k)
             {
                 nr ++;
                 aduna();
             }
             else
                bkt(poz+1);
         }
    }
}

int main()
{
read();
bkt(1);
ans /= nr;
g << fixed << setprecision(6) << ans;
    return 0;
}