본문 바로가기
공부/코딩테스트

백준: 4966번 St. Ives (Python3)

by 혼밥맨 2022. 8. 2.
반응형

St. Ives 성공다국어

 
 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 139 100 94 72.868%

풀이

입력: N 
출력: 아래 man, wives, sacks, cats, kittens의 합

  • 1 man
  • n wives
  • n*n sacks
  • n*n*n cats
  • n*n*n*n kittens

--> 1 + n + (n**2) + (n**3) + (n**4)

문제

Robert the chapman (a medieval traveling merchant) made regular trips between his home village and St. Ives to peddle his cloth, ribbons, and needles. On one such trip he encountered a curious procession:

As I was traveling to St. Ives
I met a man with seven wives.
Every wife had seven sacks.
Every sack had seven cats.
Every cat had seven kits.
Kits, cats, sacks, wives -
How many were traveling to St. Ives?

The answer to this classic ancient riddle is ’one’. Robert was traveling to St. Ives. The others were all traveling away from St. Ives. However, if we prefer to ask the question of how many were traveling from St. Ives, we can add up:

  • 1 man
  • 7 wives
  • 7*7 sacks
  • 7*7*7 cats
  • 7*7*7*7 kittens

for a total of 2801.

On his next trip to St. Ives, Robert met the same man, this time accompanied by 3 wives, each
with 3 sacks, and so on. Becoming curious about what seemed to be a bizarre village ritual of some kind, Robert kept track of how many traveled with the man each time he encountered him during the subsequent year.

On average, what was the size of the processions that Robert encounter on his trips to St. Ives?

입력

Input consists of multiple data sets. Each data set consists of a line with a single floating point number number representing the numbers of wives, sacks per wife, cats per sack, and kittens per cat that Robert encountered that year.

End of input is indicated by a value of zero.

출력

For each data set, print the size of the average procession as a real number presented to 2 decimal points precision.

예제 입력 1 

7
1
2.5
0

예제 출력 1

2801.00
5.00
64.44

답안

1
2
3
4
5
6
7
8
while(True):
    num=eval(input())
    if num==0:
        break;
    else:
        float_ = 1 + (num) + (num**2+ (num**3+ (num**4)
        format_float = "{:.2f}".format(float_)
        print(format_float)
cs

반응형

댓글