Cod sursa(job #2541825)

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

using namespace std;

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

int n, k, sol[27], lg;
double v[27], rez[1000005];
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 = aux * v[sol[i]];
   }
  lg ++;
  rez[lg] = aux;
}

void rezultat ()
{
  int i;
  double ans = 0;
   for  (i=1; i<=lg; i++)
   {
       ans += (rez[i]/nr);
   }
  g << fixed << setprecision(6) << ans;
}

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);
rezultat();
    return 0;
}