What's the reasoning behind n*n*100 in line 11? It seems you want to find a possible solution and check to see if it's bigger than or smaller than what you find from your for loops, but I can't imagine how you'd be able to come up with a plausible answer with only the number of rooms in the barn. You need to know how many cows there are too. Were you simply trying to make a large number to set off the if statement?
Honestly, I looked very briefly over your code and didn't know what you were doing (Line 19/21 - why are you multiplying?). I decided to code the solution myself for you to look at. There may be other ways of doing this, but this is the logic I used:
using namespace std;
fin >> n;
int cows = 0;
for (int i = 0; i < n; i++)
fin >> t[i]; //Get how many Cows Per Room
cows += t[i]; //Add Them up To Get TOTAL Cows
int smallest = 0;
for(int ta = 0; ta < n; ta++) //Loop as many times as we have rooms
int cow = cows;
for (int i = 0; i < n; i++) //Find distance traveled if we start at beginning
distance[ta] += (cow -= t[i]);
//Change The Beginning:
t.push_back(t); //Put The First Value At The End
t.erase(t.begin()); //Delete First Value
//^This Essentially moves the first element to the last position
if (ta == 0) //If first iteration, save the value
smallest = distance[ta];
else if (smallest > distance[ta]) //Otherwise, check if distance is smaller than last distance
smallest = distance[ta];
std::cout << "The Shortest Route Is: " << smallest;
fout << "The Shortest Route Is: " << smallest;
In my code, "t" is equivalent to your "roomcapacity" array.
The logic your code needs to follow is this:
1. I have X amount of cows.
2. Starting from room 1, I'd lose T amount of cows.
3. P amount of cows will have to go through the door to the next room (This is your distance and the amount of cows you have left, keep track).
The logic of my code is this for your input of 5 4 7 8 6 4:
4+7+8+6+4 = 29 -- we have 29 cows.
Start at the first room, we'd lose 4 cows before we even move through a door. We now have 25 cows, distance 0.
We move through the first door, distance = 25. This is the second room, lose 7 cows. We now have 18 cows.
Move through second door, distance = 25+18 = 43. This is the 3rd room, so -8 cows, we have 10 cows left.
Move through 3rd door, reach the 4th room. Distance = 43 + 10 = 53. Cows -= 6. We have 4 cows left.
Move through 4th door, reach the 5th room. Distance = 53+4 = 57. We lose 4 cows in the 5th room, so now we have 0 cows. We're done with the first iteration.
The next iteration will say, what if we start at room 2 rather than room 1? Which is what I mean by change the beginning in my code. The next iteration wont use 4 7 8 6 4, but instead will use 7 8 6 4 4 . This is the same as starting the logic from the next room over. Of course, doing the math, we know starting from the second room gives us the shortest distance of 48. You simply have to keep iterating through all the possibilities until you find the shortest distance.
For every iteration, Your Program Produces These Distances:
My Program Produces These Distances:
I did the math myself and confirmed my program's results.