Cod sursa(job #639612)

Utilizator yippeeErja Eduard yippee Data 23 noiembrie 2011 17:43:30
Problema Dezastru Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <stdlib.h>
#include <stdio.h>
#include <fstream>


using namespace std;


FILE *f;
int n,k,m,st[25],contor = 0;
double a[25],s = 0;


void solve()
{
    double p = 1;
    contor++;
    for(int i=0; i<m; i++)
     p *= a[st[i]-1];
    s += p;
}

int valid(int k, int c)
{
    for(int i=0; i<k; i++)
     if(st[i] == c)
       return 0;

   return 1;
}

void back(int k)
{
    if(k == n)
      solve();
    else
     for(int i=1; i<=n; i++)
       if(valid(k,i))
       {
           st[k] = i;
           back(k+1);
       }
}

int main()
{
    f = fopen("dezastru.in","r");
    fscanf(f,"%d %d", &n,&m);
    for(int i=0; i<n; i++)
       fscanf(f,"%lf", &a[i]);

    back(0);

    f = fopen("dezastru.out","w");
    fprintf(f,"%0.6lf", s/contor);

    return 0;
}