Hi, ok... Here's the story- I have been absent from my Pascal programming class for 3 weeks, I came back and now the teacher wants me to try out a few programs for a test tomorrow. I have one that I have no idea how to do...
Here are the directions:
"Mr.Paul has an amusement park. On one ride, there are many different cabins painted different colors. Since the colors evoke different emotions, each cabin is priced individually.
A group of friends want to ride Mr.Paul's cabin ride. They want to sit together, or in cabins right next to each other.
The amount of friends: N
The capacity of one cabin: K
The amount of cabins: M
Now, the program will get these numbers from a file on the desktop (the file should have N,K,M separated by a space, plus the $$ amount each cabin costs), then it should calculate how many cabins are needed, then which combination is the cheapest for the friends.
So, for an example:
Input: 7 2 10
80 70 40 90 100 60 70 80 80 50
Output:
240
Good luck!"
Ok, so I have absolutely no idea.
I wanted to ask you for help- please please please if anyone knows how to make this program, don't hesitate to write at least a little part of it.
Thank you!!!!
Fórum › Pascal
Pascal HELP.... please :)
Hmm... why not use brute force? You may try every possible combination and keep trace of the cheapest one. So you have four things to do:
1) Generate the combinations.
2) Check if a given combination is valid.
3) Calculate the price for a given combination.
4) Compare every calculated price with the one we remember and then remember the lower one (together with the combination).
Which of them is the biggest problem?
Moje stránka.
To Mircosoft : Exactly what I thought of... But I have no idea which commands I should use... For example, the number of cabins changes with every use of the program, so what kind of function can I use to calculate this?
If we calculate (add the cabins next to each other- every possible combination), then we can just use something like "if a>b then..."?
(Thank you so much :))
To Mircosoft [#76984]: I have to go to bed (I'm only 16, so I have a bed time :)), I guess I will just tell the teacher that I couldn't come up with it.
Thank you very much anyway...
When you have time, could you please just post the command that I should use? That way I can ask her to explain that to me?
Thanks!!
Commands? You will need Assign, Reset, Read, Readln and Close to read from a file (text file, I suppose). And a variable of type Text. Then some conditions (If), loops (For, While, Repeat) and something to compose the program of (Program, Begin, End, basic operators (:=, =, <>, and, or...) etc.).
I was thinking about the implementation. It's not so easy how it looked at first :-).
First idea:
You have some number of cabins and some seats in each of them. Store them into an array, where each element means one cabin (key words: array, record). Keep their number of seats, price, starting index (will explain later) and number of seats currently occupied.
Upon reading the input, you get the number of cabins. Keep this number - you will need it to run For loops. The array itself can be much bigger and doesn't have to be completely used-up, you just start with index 1 and end with previously read NumberOfCabins.
Then you have an array of booleans, one for every seat. True means occupied, false means non-occupied. Here you generate the combinations: just switch the bools in the way how bits in a number switch when the number is incremented:
FFFFFFFF...
TFFFFFFF...
FTFFFFFF...
TTFFFFFF...
FFTFFFFF...
TFTFFFFF...
Here's what that starting index of cabin is: first cabin will start with first seat. If it has 3 seats, the second cabin will begin on index 4. With 5 seats, the third will begin on index 3+5+1. And so on, like words in a string without spaces: 1112222233344444555555555
Again, the real size of this array must be greater than any possible total number of seats, use some variable to store actual value of max. index to this array.
Checking whether the combination is valid means that you go through array of cabins, take each one's starting index and number of seats and then count according trues (= occupied seats) in that boolean array. Store the counts to cabin array. When finished, go through the cabins again and determine whether there are any gaps - interval of empty cabins surrounded by non-empty cabins. If there are, combination is invalid. Also if total number of trues is not equal to the number of friends.
Then multiply number of occupied seats and price in each cabin, sum it together and you get total price. Compare it with lowest value you found till now and if it's lower, save it instead of the old one. And save the configuration of seats (that boolean array) too.
Then take another combination and do everything again, until all elements in the boolean array are true.
P.S.: Surely it can be done in some faster and more ingenious way, but I have no idea how at this moment.
Moje stránka.
Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Uživatelé prohlížející si toto vlákno
Podobná vlákna
Dev Pascal, Free pascal - oba mi spustí program 2x po sobě — založil Gooo
Pomoc:pascal:kurz turbo pascal II — založil Systém
Pascal: kurz turbo pascal II — založil Systém
Moderátoři diskuze