티스토리 뷰
Five pirates are shipwrecked on a desert island. They quickly determine that the only other inhabitant of the island is a monkey and that the only food is coconuts. They set about collecting as many coconuts as they can and put them all in a pile. By nightfall they are too tired to divide the harvest, so they agree to go to sleep and divvy up the coconuts the next morning.
During the night one pirate awakens, suspicious that the others might try to cheat him, and decides to take his portion then and there and not wait until morning. He divides the coconuts into five piles and finds there is one coconut left over, which he gives to the monkey. He hides one of the five piles, then puts the rest of the nuts together and returns to sleep.
About an hour later a second pirate awakens with the same suspicions and does the same thing: he divides the coconuts into five piles, leaving one extra, which he gives to the monkey. Then he hides what he thinks is his share and goes back to sleep. One after another the rest of the pirates do the same: they each take one fifth of the coconuts in the pile (after giving the extra one to the monkey) and then return to sleep.
When the pirates awaken the next morning they all notice the coconut pile is much smaller than it was the night before. But since each man is as guilty as the others, no one says anything. They divide the coconuts for the sixth (and last) time, but this time there is no coconut left for the monkey.
How many coconuts were in the original pile?
Assignment
The answer to the above puzzle is 3121. You may look ahead to the first example below to check that this is indeed the correct answer. But what if there were six pirates, or seven, or thirty? What is the correct answer in those cases? In this assignment we check how
coconuts can be divided among
pirates, and how many coconuts are left for the monkey, if the pirates apply the procedure as outlined in the above puzzle.
Input
The input contains the number of pirates
and the number of coconuts
the pirates have initially collected, each on a separate line.
Output
For each pirate, output a single line that is formatted as:
a nut(s) = b nut(s) for pirate#n and c nut(s) for the monkey
This indicates the number of coconuts the pirate takes as his share during the night, and how many nuts he gives to the monkey. The fragments in italic need to be filled up with the following values:
- is the number of coconuts the pirate finds in the pile when he wakes up during the night
- is the number of coconuts the pirate hides during the night
- is the number of coconuts the pirate gives to the monkey during the night
- is the sequential number of the pirate (pirates are numbered #1, #2, …)
Use the correct singular or plural form in outputting a number of coconuts:
number of coconuts | is outputted as |
---|---|
0 nut(s) | no nuts |
1 nut(s) | 1 nut |
n nut(s) with | n nuts |
Finally, another line needs to be outputted that indicates how the remaining coconuts are divided among the pirates and the monkey when morning breaks. This line needs to be formatted as:
each pirate gets d nut(s) and e nut(s) for the monkey
The fragments in italic need to be filled up with the following values:
- is the number of coconuts each pirate gets in the morning
- is the number of coconuts the monkey gets in the morning
Again, in outputting a number of coconuts, the correct singular or plural form must be taken into account as indicated in the above table.
Example
This example corresponds to the puzzle as given in the introduction.
Input:
5 3121
Output:
3121 nuts = 624 nuts for pirate#1 and 1 nut for the monkey 2496 nuts = 499 nuts for pirate#2 and 1 nut for the monkey 1996 nuts = 399 nuts for pirate#3 and 1 nut for the monkey 1596 nuts = 319 nuts for pirate#4 and 1 nut for the monkey 1276 nuts = 255 nuts for pirate#5 and 1 nut for the monkey each pirate gets 204 nuts and no nuts for the monkey
Example
Input:
4 1234
Output:
1234 nuts = 308 nuts for pirate#1 and 2 nuts for the monkey 924 nuts = 231 nuts for pirate#2 and no nuts for the monkey 693 nuts = 173 nuts for pirate#3 and 1 nut for the monkey 519 nuts = 129 nuts for pirate#4 and 3 nuts for the monkey each pirate gets 96 nuts and 3 nuts for the monkey
ver. latest
def cal_nuts(nuts, num_pirate):
return int(nuts / num_pirate), nuts % num_pirate
def get_form(nuts):
if nuts > 0:
return str(int(nuts)) + " nut" + ("s" if nuts > 1 else "")
return "no nuts"
def get_info_cur(i, nuts, nuts_pirate, monkey):
return "{} = {} for pirate#{} and {} for the monkey"\
.format(get_form(nuts), get_form(nuts_pirate), str(i + 1), get_form(monkey))
def get_info_final(nuts_pirate, monkey):
return "each pirate gets {} and {} for the monkey"\
.format(get_form(nuts_pirate), get_form(monkey))
def show(i, nuts, num_pirate, final):
nuts_pirate, nuts_monkey = cal_nuts(nuts, num_pirate)
if final:
print(get_info_final(nuts_pirate, nuts_monkey))
else:
print(get_info_cur(i, nuts, nuts_pirate, nuts_monkey))
return nuts - (nuts_pirate + nuts_monkey)
def get_coconut(num_pirate, nuts):
for i in range(num_pirate):
nuts = show(i, nuts, num_pirate, False)
show(i, nuts, num_pirate, True)
get_coconut(int(input()), int(input()))
def get_number_nuts(nuts, number_pirate):
monkey = nuts % number_pirate
nuts_pirate = (nuts - monkey) / number_pirate
return nuts_pirate, monkey
def get_str_nuts(nuts):
if nuts > 1:
return str(int(nuts)) + " nuts"
elif nuts == 1:
return str(int(nuts)) + " nut"
else:
return "no nuts"
def get_current_info(i, nuts, nuts_pirate, monkey):
return get_str_nuts(nuts) \
+ " = " + get_str_nuts(nuts_pirate) \
+ " for pirate#" + str(i+1) + " and " \
+ get_str_nuts(monkey) + " for the monkey"
def get_final_info(nuts_pirate, monkey):
return "each pirate gets " + get_str_nuts(nuts_pirate) + " and " + get_str_nuts(monkey) + " for the monkey"
def cal(number_pirate, nuts):
for i in range(number_pirate):
nuts_pirate, monkey = get_number_nuts(nuts, number_pirate)
print(get_current_info(i, nuts, nuts_pirate, monkey))
nuts -= (nuts_pirate + monkey)
nuts_pirate, monkey = get_number_nuts(nuts, number_pirate)
print(get_final_info(nuts_pirate, monkey))
cal(int(input()), int(input()))
'python lecture > algorism' 카테고리의 다른 글
[eud] Cowsay (0) | 2018.10.02 |
---|---|
[edu] Wepe speapeak p (0) | 2018.10.02 |
[edu] Pythagorean triples (0) | 2018.10.02 |
[edu] 성능 시작측정 코드(decorator, prime, 소수) (0) | 2018.08.27 |
[edu] 소수 구하기 (0) | 2018.08.27 |
- Total
- Today
- Yesterday
- 면접답변
- admin.py
- 장고 플러스친구 자동응답
- 모바일 테마 적용
- 모바일 스킨 적용
- 파이썬 입문
- pycrypto
- gitlab
- 파이썬
- 엑셀 비교
- django
- 문과 코딩
- virtualenv
- chatbot
- 파이썬 독학
- 플러스친구 자동응답
- 장고
- 문서 비교
- 파이썬 강좌
- wsgi
- GIT
- 면접정답
- 이미지 비교
- Python
- Tistory
- 장고 카톡 자동응답
- PuTTYGen
- django chatbot
- 파이썬 프로그래밍
- gitignore
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |