Info

此问题已关闭。 请重新打开它进行编辑或回答。

how to do while loops with actual problems

1 次查看(过去 30 天)
this is the most i have
n=1;
orders = [5 2 3 2.5 1 .8 1.2];
oil_supply=0;
oil_reserves = 15;
% number of full orders i can do and its remainder
while oil_supply < oil_reserves;
oil_supply = oil_supply + orders(n);
n=n+1;
Amount_left_in_Tank = oil_reserves - 15
end
fprintf('amount of orders completed %d\n',n)
fprintf('what left in the tank %d\n',Amount_left_in_Tank)
  1 个评论
John D'Errico
John D'Errico 2019-6-15
编辑:John D'Errico 2019-6-15
Please stop adding answers. Use comments, as you are not answering your question. Or edit your question, adding this as information.

回答(4 个)

James Browne
James Browne 2019-6-15
Greetings,
I believe I have a solution to your problem. First, let us consider the nature of the problem...We have a certain amount of oil on hand, that we can use to fill customer orders. We also have a set of orders which request various amounts of oil and we want to fill as many full orders as we can with the reserves that we have on hand. We therefore first need to figure out how to determine the combination of orders that will give us the highest number of full orders satisfied while minimizing the amount of oil needed to satisfy all of the orders.
The most efficient solutions is then to first arrange the oil requests in the order of least amount requested to highest amount requested. We then add up the amount of oil that is needed to satisfy all orders from 1 to n. When the amount of oil needed to satisfy all orders exceeds or is equal to the amount of oil we have on hand, we stop. If the amount of oil needed to satisfy all orders is equal to the amount we have on hand for n orders, n is the number of full orders that we can fill, with the reserves on hand. If the amount of oil needed to satisfy n orders is greater than the reserves on hand, n-1 is the number of full orders that we can fill, with the reserves on hand.
This analysis can be executed with a while loop but it is easier (in my opinion) to execute with a for loop, as follows:
%State the amount of oil on hand
reserves = 15000;
%State the orders (requests for oil)
oilRequests = [5000 2000 3000 2500 1000 800 1200];
%Arrange oil requests in ascending order
oilRequests = sort(oilRequests);
%Determine the number of requests (n)
n = length(oilRequests);
%Determine the highest number of full orders that can be filled, given the
%reserves on hand
proceed = true;
totalRequested = 0;
nOrdersCanBeFilled = 0;
for i = 1:n
if ( proceed )
totalRequested = totalRequested + oilRequests(i);
if ( totalRequested > reserves )
nOrdersCanBeFilled = i - 1;
proceed = false;
end
if ( totalRequested == reserves )
nOrdersCanBeFilled = i;
proceed = false;
end
end
end
%Determine total amount of oil needed to fill maximum number of full orders
%this will verify that the amount of oil needed to fill the stated number
%of orders that can be filled will not exceed the oil reserves on hand
totalNeeded = sum(oilRequests(1:nOrdersCanBeFilled));
fprintf('The total number of full orders that can be filled, given the reserves of\n')
fprintf('is %i.\n',nOrdersCanBeFilled)
fprintf('Filling these orders will require %d of the given %d oil reserves on hand',totalNeeded,reserves)
Hope this answers your question~

Ankit Kumar Mishra
Ankit Kumar Mishra 2019-6-15
编辑:Ankit Kumar Mishra 2019-6-15
While loop is not the problem in this code.
There appears to be logical error in your solution. Your solution will not give the intended result as the variable
'Amount_left_in_Tank' will reduce to zero in first iteration of loop itself. This problem requires greedy approach. You need to sort your order array in ascending order so that small orders get completed first and number of orders fulfilled can be maximized.
Use this modified code instead.
n=1;
orders = [5 2 3 2.5 1 .8 1.2];
oil_supply=0;
oil_reserves = 15;
%finding length of array. col is no. of element in this array
[row,col]=size(orders);
%sort orders in ascending order so that small orders get fullfilled first so that number of orders is maximized
sorted_orders=sort(orders);
% number of full orders i can do and its remainder
while oil_supply < oil_reserves && n<=col;
oil_supply = oil_supply + sorted_orders(n);
%check if oil_supply does not exceed the reserves else break out of loop.
if oil_supply<=oil_reserves
%Amount left in tank is the same as the (initial oil reserve- the oil supplied till now)
Amount_left_in_Tank = oil_reserves - oil_supply;
n=n+1;
else
%break is used to terminate the loop and come out of it.
break;
end
end
% n is already initialized to one before so total no. of orders completed should be n-1
fprintf('amount of orders completed %d\n',(n-1))
fprintf('what left in the tank %d\n',Amount_left_in_Tank)

Andres Rojas
Andres Rojas 2019-6-15
编辑:Andres Rojas 2019-6-15
thank you all very much,,
this is way more than ive ever gotten at school.
problem is i dont recognize some of the codes you guys are using so it makes it a bit more confusing, im trying to pick it up slow and smooth ......
we havent been taugh anything but if statments and while loops...
i dont recognize the codes you guys have provided .
i asked on another cite, for help but to keep it as simple as possible this is what i was provided and i struggle less to read this one minus the use of count?
is it a predefined word in matlab or how and why is count used here ?
order = [ 5000 2000 3000 2500 1000 800 1200];
i = 1;
s = 0; count = 0;
while i<=7
s = s + order(i);
if s <= 15000
count = count + 1;
end
i = i + 1;
end
s = 0; i =1;
while i<=count
s = s + order(i);
i = i + 1;
end
left = 15000 - s;
fprintf('orders satisfied = %d\n', count);
lastly i apologize but i have no idea how to make my comments show up the way they were edited , im new please calm down. thanks

Andres Rojas
Andres Rojas 2019-6-15
dropped course !

此问题已关闭。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by