Cod sursa(job #639604)

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


using namespace std;


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

void type()
{
    for(int i=0; i<n; i++)
     printf("%d ",st[i]);
    printf("\n");
}

void solve()
{
    float 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();
      //type();
    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,"%f", &a[i]);


    back(0);

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

    return 0;
}