首页 > 试题广场 >

n的阶乘

[编程题]n的阶乘
  • 热度指数:48803 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个整数n,输出n的阶乘(每组测试用例可能包含多组数据,请注意处理)

输入描述:
一个整数n(1<=n<=20)


输出描述:
n的阶乘
示例1

输入

3

输出

6
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);
    }
}


发表于 2021-02-04 16:26:18 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        // 使用int 会溢出
        long res=1;
        for (int i = 2; i <=n; i++) res*=i;
        System.out.println(res);
    }
}


发表于 2020-03-17 16:08:16 回复(0)
  • 一开始用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);
      }
    }
发表于 2020-02-22 11:19:33 回复(0)
方法一:使用C++但是本人不会大整数,但是这题使用long long竟然能过,一般考研复试都可以使用Java,所以Java中的大整数一定要会,那么先贴一下我的弱智C++解法吧。。。

刚才在Xcode上面试了一下21 long long 就不行了,所以为了考研复试还是推荐使用Java解决大整数问题,
#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;

}

方法二:Java(推荐使用)
Python考研复试一般都不支持,所以尽量不要使用Python处理。。。

代码如下:

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);
    }
}


编辑于 2020-02-06 00:20:23 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long n = sc.nextInt();
            long sum = 1;
            for(int i=1;i<=n;i++){
                sum*=i;
            }
            System.out.println(sum);
        }
    }
}

发表于 2018-10-19 09:04:56 回复(0)

居然还有这种事情。。。脑补微博[跪了]表情 /笑哭
图片说明

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]

编辑于 2018-04-15 13:21:50 回复(0)