import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args) {
//Scanner sc = new Scanner(System.in);//运行时间:38ms 占用内存:10892KB
//int n = sc.nextInt();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//运行时间:8ms 占用内存:9420KB
int n= 0;
try {
n = Integer.parseInt(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
long product = 1;
for (; n > 0; n--) {
product = product * n;
}
System.out.println(product);
}
}
一开始用int保存结果,但测试发现int根本装不下那么大的数,需要用long
除此之外,因为不是大数阶乘,所以题目相对较基础,一般作为递归的入门例子
import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
int n=sc.nextInt();
System.out.println(result(n));
}
}
private static long result(int num)
{
if(num==1)
{
return 1;
}
return num*result(num-1);
}
} #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
ll ans=1;
for(int i=1;i<=n;i++){
ans*=i;
}
cout<<ans<<endl;
return 0;
} import java.util.*;
import java.math.BigInteger;
public class Main{
public static void main(String[] args){
BigInteger num=new BigInteger("1");
BigInteger ans=new BigInteger("1");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++){
ans=ans.multiply(num);
num=num.add(new BigInteger("1"));
}
System.out.println(ans);
}
} 居然还有这种事情。。。脑补微博[跪了]表情 /笑哭
import java.util.Scanner;
public class Main {
static long factorial(int n) {
if(n == 0) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()) {
int n = scan.nextInt();
System.out.println(factorial(n));
}
}
}
10s后再次提交,ojbk,[doge]