简单的循环。因为给的输入范围在之内,所以并不需要用到复杂的算法。
#include <bits/stdc++.h>
int main() {
int n;
while (std::cin >> n){
long product = 1;
for(int i=2;i<=n;i++){
product*=i;
}
std::cout << product << std::endl;
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
long product = 1;
for (int i = 1; i <= n; i++) {
product *= i;
}
System.out.println(product);
}
}
}