Tyson has to finish spending the section budget soon or his boss will reduce the budget for next quarter. Thus, he has to spend $25,000 in the next week, and the only items that would arrive in time are these:
What is the largest amount that Tyson can spend?
If you liked this problem on Integer Programming, you should try Part 1 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Is this even a computer science problem? I solved it using simple algebra
I used linear programming; if you model it as the equation
1579x + 5899y = 25000
, where x = workstations and y = servers and graph that line, you get a triangle with respect to the x and y axes. Then, if you take the slope of the line connecting the origin to the point where the intercepts intersect and use that to find the equation of the line in slope-intercept form, you can graph that along with the the original 'triangle' and the point at which your new line intersects the first line is the largest amount of both that you could buy, so you would take the point of intersection (x, y) and multiply x and y by their respective prices above and add them together. Linear programming is used for optimization in cases such as this.
One way is to model the problem as an integer programming problem:
Maximize 1 5 7 9 x + 5 8 9 9 y
subjected to
1 5 7 9 x + 5 8 9 9 y ≤ 2 5 0 0 0
x , y integers
And use the branch and bound framework to solve it.
This is a good option to use LINEAR PROGRAMMING
Python:
list=[]
for i in range(16):
for j in range(4):
if 1579*i+5899*j<25000:
list.append(1579*i+5899*j)
print(list)
From here I just found the biggest number in "list"
print(max(list)) Would be more appropriate Nice soln Btw
Here is my solution in Python:
amount = 0 #variable that calculates all amounts under 25000
values = [] #this array stores all amounts under 25000
for a in xrange(0, 20):
for b in xrange (0, 20):
amount = (a*1579) + (b*5899) #calculate the value for amount
if amount <= 25000:
values.append(amount) #add amount to values array if less than 25000
print max(values)
The programme returns 24847 as the answer.
i = 0
X = 0
Y = 0
while i <= 15:
j = 0
while j <= 4:
X = i*1579 + j*5899
if X < 25000:
print X
j = j + 1
i = i + 1
print Y
void main() { long i,j,k=0; long price[256],val=0,max; for(i=0;i<16;i++) { for(j=0;j<16;j++) { val=i 1579+j 5899; if(val<=25000) { price[k++]=val; } } } max=price[0]; for(i=0;i<k;i++) { if(price[i]>max) { max=price[i]; } } printf("max value=>%ld",max); }
A quick solution in C++ which will slightly reduce the complexity of the algorithm:
#include <iostream>
using namespace std;
int main() {
int a, b, big, biggest = -1;
cin >> a >> b >> big;
for (int i = 0; i <= big/a; i++) {
for (int j = 0; j <= (big-a*i)/b; j++) {
if (biggest < a*i+b*j && a*i+b*j < big) {
biggest = a*i+b*j;
cout << biggest << '\n';
}
}
}
return 0;
}
*Solution in C++ : *
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int cost = 0, totalCost[1000], c = 0;
int cost_of_workstation = 1579;
int cost_of_server = 5899;
for(int I = 0; I <= 100; I++)
{
for(int J = 0; J < 100; J++)
{
cost = I * cost_of_server + J * cost_of_workstation;
if(cost <= 25000)
totalCost[c++] = cost;
}
}
int maxCost = totalCost[0];
for(int I = 0; I < c; I++)
{
if(totalCost[I] > maxCost)
maxCost = totalCost[I];
}
cout << maxCost << endl;
return 0;
}
My Node JS solution, its generic and solves any of these kind of problems, I wish i can still reduce the complexity
input : node max_exp.js 25000 5899 1579; output : result : 1*5899 , 12*1579 = 24847
var final_total = process.argv[2] || 0,
items = process.argv.splice(3) || [],
num_items = items.length;
var max_count = 0,
result;
var cal = function(total, item_price, children, count_arr){
var max_mul = Math.floor(total/item_price);
var is_first_item = (children.length + 1) == num_items,
is_last_item = children.length == 0;
for(var i = 0; i <=max_mul; i++){
if(is_first_item) {
count_arr = [];
}
count_arr[num_items - (children.length + 1)] = i;
var remaining_price = total - i*item_price;
if(is_last_item){
var cal_max_count = final_total - remaining_price;
if(max_count < cal_max_count){
max_count = cal_max_count;
result = get_str(count_arr) + " = " + max_count;
}
continue;
}
cal(remaining_price, children[0], children.slice(1), count_arr);
}
};
// this gets the string to display result
var get_str = function(count_arr){
var str = "";
count_arr.forEach(function(c, i){
str += c + "*" + items[i] + " , ";
});
return str.substr(0, str.length - 3);
};
cal(final_total, items[0], JSON.parse(JSON.stringify(items)).splice(1));
console.log("result : " + result);
int i,j; int m=o,a=0; for(i=0;i<5;i++) { for(j=0;j<14;j++) { a=5899 i+1579 j; if(m>a&&a<25000) a=m; } } cout<<"maximum value is: "<<m;
Problem Loading...
Note Loading...
Set Loading...
solution in java:
public int maximaze() { int a=(int) Math.floor(25000/1579); int b=(int) Math.floor(25000/5899); List<Integer> list= new ArrayList<Integer>();