// 5.1.js // 2024-11-02 // $Id: 5.1.js 1.1 2024/11/02 11:30:48 s Exp $ // initialization ol = new Array(6); pw = new Array(6); npv = new Array(6); x = [[],[],[],[],[],[],[],[],[],[], [],[],[],[],[],[],[],[],[],[], [],[],[],[],[],[],[],[],[],[], [],[],[],[],[],[]]; scost = new Array(32); snpv = new Array(32); comment = new Array(32); comment.fill("-"); // input ol[1] = 100; ol[2] = 300; ol[3] = 200; ol[4] = 150; ol[5] = 150; pw[1] = 200; pw[2] = 500; pw[3] = 300; pw[4] = 200; pw[5] = 250; // calculation for(i=1; i<=5; i++){ npv[i] = pw[i] - ol[i]; } ns=0; snpvmax=0; for(i=0; i<=1; i++){ for(j=0; j<=1; j++){ for(k=0; k<=1; k++){ for(l=0; l<=1; l++){ for(m=0; m<=1; m++){ x[ns][1] = i; x[ns][2] = j; x[ns][3] = k; x[ns][4] = l; x[ns][5] = m; scost[ns] = ol[1]*x[ns][1] + ol[2]*x[ns][2] + ol[3]*x[ns][3] + ol[4]*x[ns][4] + ol[5]*x[ns][5]; snpv[ns] = npv[1]*x[ns][1] + npv[2]*x[ns][2] + npv[3]*x[ns][3] + npv[4]*x[ns][4] + npv[5]*x[ns][5]; if(scost[ns]>600){ comment[ns]="cost>600"; } else if(snpv[ns] > snpvmax){ snpvmax = snpv[ns]; } ns++ } } } } } for(i=0; i<=31; i++){ if(scost[i]<=600 && snpv[i]==snpvmax){ comment[i] = "optimal"; } } // output console.log("5.1.js"); console.log("ns".padStart(3), "x1".padStart(3), "x2".padStart(3), "x3".padStart(3), "x4".padStart(3), "x5".padStart(3), "cost".padStart(5), "npv".padStart(4), "comment"); hr(43); for(i=0; i<=31; i++){ console.log( i.toFixed(0).padStart(3), x[i][1].toFixed(0).padStart(3), x[i][2].toFixed(0).padStart(3), x[i][3].toFixed(0).padStart(3), x[i][4].toFixed(0).padStart(3), x[i][5].toFixed(0).padStart(3), scost[i].toFixed(0).padStart(5), snpv[i].toFixed(0).padStart(4), comment[i] ); } hr(43); function hr(n){ dash ="" for(i=1; i<=n; i++) dash = dash+ "-" console.log("%s",dash); } // end