Unexpected Factorial

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 32M

Authors:
Problem types
Allowed languages
C, C++

In mathematics, the factorial of an integer (denoted by an ! symbol) is as follows:

0! = 1

1! = 1

2! = 2 \times 1 = 2

3! = 3 \times 2 \times 1 = 6

4! = 4 \times 3 \times 2 \times 1 = 24

5! = 5 \times 4 \times 3 \times 2 \times 1 = 120

On Reddit, there is a subreddit /r/unexpectedfactorial, dedicated to trolling users who have accidentally included a factorial in their post or comment.

Here are some examples of unexpected factorials:

Original: By 2007, I was only 5!

Response: Wow, 120 years old is quite old.

Original: Calculators explode when dividing by 0!

Response: Hmm, dividing by 1 shouldn't make your calculator explode...

Input Format

Your program is required to detect an unexpected factorial N! (0 \le N \le 20) in a single line of text input, and output the actual value of N! (luckily for you, you are not required to phrase it into a statement). If the input does not contain any unexpected factorial, output -1. The input may contain spaces, punctuation and numbers.

The input should only contain 1 unexpected factorial, but may contain multiple exclamation marks.

Output Format

Your program should output a single integer, the full actual value of N!.

Constraints

Subtask 1 (0%): Sample Testcases.

Subtask 2 (30%): 0 \le N \le 10.

Subtask 3 (70%): 0 \le N \le 20.

Sample Input 1

Nice! I rate 5/7!

Sample Output 1

5040

Sample Input 2

How obvious - how necessary - was the mathematical ratio of its sides, the quadratic sequence 1:4:9! (from 2001: A Space Odyssey)

Sample Output 2

362880

Sample Input 3

This string does not contain any unexpected factorial!

Sample Output 3

-1

Comments