Cod sursa(job #37921)

Utilizator ZweisteinAdrian VELICU Zweistein Data 25 martie 2007 13:04:48
Problema Dezastru Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <stdio.h>
#include <memory.h>
int n,k;
long double prob[28];

int sol[28];
char uzed[28];

long double totprob;
long double curprob=1;
long totcount;

void woosh(int ou) {
	if (ou==k+1) {
		/*
		for (int i=1; i<=k; i++) printf("%Lf ",prob[sol[i]]);
		printf("\n");
		*/
//		long double localprob=1;
//		for (int i=1; i<=k; i++) localprob*=prob[sol[i]];
	       	// printf("Local probability is %Lf\n",localprob);
		totprob+=curprob;
		totcount++;	
	} else {
		for (int i=1; i<=n; i++) if (!uzed[i]) {
			uzed[i]=1;
			sol[ou]=i;
			curprob*=prob[sol[ou]];
			woosh(ou+1);
			curprob/=prob[sol[ou]];
			sol[ou]=0;
			uzed[i]=0;
		};
	};
};
int main (void) {
	FILE * fi = fopen("dezastru.in","rt");
	FILE * fo = fopen("dezastru.out","wt");
	fscanf(fi,"%d %d",&n,&k);
	for (int i=1; i<=n; i++) fscanf(fi,"%Lf",&prob[i]);
	for (int i=1; i<=n; i++) sol[i]=i;
	memset(uzed,0,sizeof(uzed));
	woosh(1);
	fprintf(fo,"%.6Lf\n",totprob/totcount);
	fclose(fi); fclose(fo);
	return 0;
};