简体   繁体   中英

Optimize a program that calculates the profit-maximizing number of apartments to rent

For my C++ class, one of the programming exercises is to build a program that, given certain variables, returns the profit-maximizing number of apartments to rent. My code works, except the answer is one lower than the correct answer would be. Here's my code:

   // ch5ProgExercise28.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    int totalUnits, occupiedUnits, vacantUnits;
    double rentAllOccupied, rentIncreaseVacant, maintenance, oldProfit, newProfit;

    cout << "Enter the total number of units, the rent to occupy all the units,"
        << " the increase in rent that results in a vacant unit, and the amount"
        << " to maintain a rented unit.";
    cin >> totalUnits >> rentAllOccupied >> rentIncreaseVacant >> maintenance;
    oldProfit = ((rentAllOccupied)*totalUnits) - (maintenance*totalUnits);
    occupiedUnits = totalUnits;
    vacantUnits = totalUnits - occupiedUnits;
    do
    {
        oldProfit = (rentAllOccupied + (rentIncreaseVacant*vacantUnits))*
            occupiedUnits - (maintenance*occupiedUnits);
        occupiedUnits--;
        vacantUnits = totalUnits - occupiedUnits;
        newProfit = (rentAllOccupied + (rentIncreaseVacant*vacantUnits))*
            occupiedUnits - (maintenance*occupiedUnits);
    } while (oldProfit < newProfit);
    occupiedUnits += 1;
    cout << "To maximize profits, " << occupiedUnits << " units will be rented." << endl;
    cin >> oldProfit; //stops the program from exiting right away
    return 0;
}

Is there a better loop structure I can use so that the occupiedUnits number is correct without having to add 1 to it for the correct answer? Thanks.

The issue you are running into is that your loop condition depends on newProfit and the newProfit calculation requires occupiedUnits - 1. You would need to either change the loop condition or the calculation of newProfit to get what you want. One way to do this would be to loop through all values of occupiedUnits and store the max profit and units in new variables. You didn't provide any test conditions, but something like this should work:

double maxProfit = 0;
int maxUnits = 0;

while(occupiedUnits > 0)
{
    oldProfit = (rentAllOccupied + (rentIncreaseVacant*vacantUnits))*
        occupiedUnits - (maintenance*occupiedUnits);
    occupiedUnits--;
    vacantUnits = totalUnits - occupiedUnits;
    newProfit = (rentAllOccupied + (rentIncreaseVacant*vacantUnits))*
        occupiedUnits - (maintenance*occupiedUnits);

    if (newProfit > maxProfit)
    {
        maxProfit = newProfit;
        maxUnits = occupiedUnits + 1;
    }
}
cout << "To maximize profits, " << maxUnits << " units will be rented." << endl; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM