即便是这样的简单题,也不能保证一遍AC过……
数据类型应为long,int型不够
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long sum=1;
for(int i=1;i<=n;i++)
{
sum=sum*i;
}
System.out.println(sum);
}
}
将输入改了之后,运行时间明显加快了
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*Scanner sc=new Scanner(System.in);
int n=sc.nextInt();*/
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String line=bf.readLine();
int n=Integer.parseInt(line);
long sum=1;
for(int i=1;i<=n;i++)
{
sum=sum*i;
}
System.out.println(sum);
}
}