Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- struct maquina{
- double prob;
- int custo;
- };
- double result[400][1000];
- struct maquina lista[1024];
- int dinheiro;
- int maquinas;
- double max(){
- int i,j;
- for(i=0;i<dinheiro;i++){
- result[0][i] = 1;
- }
- double var = 0;
- for(i=1;i<=maquinas;i++){
- for(j=1;j<=dinheiro;j++){
- result[i][j] =0;
- int k,u=100;
- double s =1,z;
- if(i==0){
- }else if(i==1 && j<lista[i-1].custo){
- }else if(i==1 && j==lista[i-1].custo){
- result[i][j] = lista[i-1].prob;
- }else if(j==1){
- if(lista[i-1].prob > result[i-1][j]){
- result[i][j] = lista[i-1].prob;
- }else{
- result[i][j] = result[i-1][j];
- }
- }else {
- int maxmaq = dinheiro/lista[i-1].custo;
- for(k=0;k<=j;k++){
- s *= 1 - lista[i-1].prob;
- u = k*lista[i-1].custo;
- z = result[i-1][j-u];
- var = (1-s)*z;
- if(var>result[i][j]){
- result[i][j] = var;
- } else {
- printf("\nresult = %.12lf var = %.12lf\n",result[i][j], var);
- }
- }
- }
- }
- }
- return result[maquinas][dinheiro];
- }
- /*
- double max(int i, int w){
- int teste = w,h;
- double sem=0,com,t=1;
- if(w==0){
- return 0;
- } else if(i==0){
- return 1;
- } else if(i==1){
- while(teste >= lista[i-1].custo){
- t = t * (1 - lista[i-1].prob);
- teste -= lista[i-1].custo;
- }
- t = 1 - t;
- return t;
- } else {
- if(w == 1){
- if(lista[i-1].prob > result[i][w]){
- sem = lista[i-1].prob;
- printf("\n sem = lista.prob = %.12f\n",sem);
- } else{
- sem = result[i][w];
- printf("\n sem = result[i][w] = %.12f\n",sem);
- }
- return sem;
- }
- while(teste >= lista[i-1].custo){
- t = t * (1 - lista[i-1].prob);
- teste -= lista[i-1].custo;
- }
- t = 1 - t;
- sem = result[i][w];
- for(h=1;h<w;h++){
- /*while(teste >= lista[i-1].custo){
- t = t * (1 - lista[i-1].prob);
- teste -= lista[i-1].custo;
- }
- t = 1 - t;
- t = result[i][h] * result[i-1][w-h];
- if(t > com){
- com = t;
- }
- }
- if(sem > com){
- printf("VOU RETORNAR SEM = %f\n",sem);
- return sem;
- } else {
- printf("VOU RETORNAR COM = %f\n",com);
- return com;
- }
- }
- }
- */
- int main(void) {
- //inputs
- int i,w;
- scanf("%d",&maquinas);
- for(i = 0;i<maquinas;i++){
- scanf("%lf %d",&lista[i].prob,&lista[i].custo);
- }
- scanf("%d",&dinheiro);
- //codigo
- int a,j;
- double f = max();
- for(i = 0;i<maquinas;i++){
- printf("linha %d:",i);
- for( j=0;j<dinheiro;j++){
- printf(" %lf,",result[i][j]);
- }
- printf("\n");
- }
- //outputs
- printf("%.12f",f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement