Description [title]
Enter a number n, you need to output n!.
! N where n represents a factorial, namely:! N = n * (n-1) * ... * 1.
For example, 5! = 4 * 5 * 3 * 2 * 1 = 120.
[Input Format]
Only one input integer n (1 <= n <= 10).
[Output format]
Output "scared I picked up the holding (n times) My little carp I (n times)." The results per line.
[Sample input]
5
[Sample output]
120
Sample [explain]
Here we enter 5, then our output is 5! , I.e., 120.
[Data] scale
50% of the data satisfies 1≤n≤5;
100% data satisfy 1≤n≤10.
[Analysis] title
This question is an entry-cycle problem, we only need to set a variable n, initially n == 1, then open a for loop, once let n multiplied by 2,3, ..., n will be able to get the final the results.
We can use the recursive way to write.
#include <bits/stdc++.h> using namespace std; int n, ans = 1; int main() { cin >> n; for (int i = 1; i <= n; i ++) ans *= i; cout << ans << endl; return 0; }
It can also be used recursively to complete. Here we func (int x) for returning x !:
#include <bits/stdc++.h> using namespace std; int n, ans = 1; int func(int x) { if (x == 1) return 1; return x * func(x-1); } int main() { cin >> n; cout << func(n) << endl; return 0; }