Learn Computer Programming Here ... and maybe represent Ireland?
You are not logged in.
(a variant of this task was asked at Microsoft interviews to select candidates with exceptional logical deduction)
The pirate crew of the Jolly Roger ship have found a hoard of treasure and decide to divide it among themselves. Each pirate has a unique rank and the highest rank pirate gets to suggest how the gold should be divided. The pirates then vote on his plan. His plan is accepted if at least half the crew vote in favour of it. Otherwise, there is a mutiny and that pirate is made walk the plank. The next highest ranked pirate then takes over and the
proposal/voting procedure is repeated.
All pirates always act logically, and try to avoid walking the plank while maximising their own share of the gold.
Given that there are G gold coins in the treasure chest and P pirates in the crew, how would you suggest dividing the gold if you were the top ranked pirate?
Write a computer program to input two integers, G and P, and to output P integers, indictating the division of gold that you would suggest for the P pirates (The pirates are ordered highest to lowest, so as the highest ranked pirate, the number of
gold coins you get should be the first integer)
Examples:
(1) G=5 P=2
Answer = 5 0
You should suggest that you get all the gold. Since you would vote for your own plan, that counts as "at least half the crew", therefore it will be accepted.
(2) G=10 P=3
Answer = 9 0 1
You should suggest that you get 9 gold coins and the bottom ranked pirate gets 1. Let's number the pirates 1 to 3, 1=highest ranked, 3=lowest ranked. If your plan fails, then pirate 2 will take all the gold, so it is in the interest of pirate 3 to vote for your plan, since he gets 1 coin (otherwise he would get nothing).
Offline
I've updated the task description with the following bounds:
You can assume the input will be two positive integers within these ranges:
G <= 1,000,000
P <= 1,000
(G is gold, P is pirates)
Offline
what should happen if the number of pirates is bigger than the amount of gold?
keep none for yourself?
Offline
Good question.
I have updated the task description and added another constraint on the amount of Gold.
G > (0.5 x P)
This means that you can assume that the amount of Gold is strictly greater than half the number of pirates.
If there are 9 pirates, P = 9, G > 4.5.
So for P=9, you can assume there are at least 5 pieces of Gold.
Offline
Hi everyone i am also new here. Looking for a great time here.
Offline