首页 > 试题广场 >

n的阶乘

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

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


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

输入

3

输出

6

python 一行代码:

import math
while True:
    try:
        print(math.factorial(int(input())))
    except:
        break
发表于 2017-09-08 10:43:56 回复(3)
#include<iostream>
using namespace std;
int main()
{
    int n;
    int a[11] ={0};//用于保存阶乘结果
    int t;
    while(cin >> n)
    {
        t = 9;//t是阶乘中间结果的最高位的前一位,用于标记
        a[10] = 1;//阶乘结果初始为1
        for(int i = 0; i < 10; i++)//初始化数组
            a[i] = 0;
        for (int i = 1; i <= n; i++)//计算阶乘
        {
            for(int j = 10; j > t; j--)//把阶乘的中间结果都乘i
                a[j] *= i;
            for(int j = 10; j > t; j--)//超过一万的进位
            {
                while(a[j] > 10000)
                {
                    a[j - 1] += a[j] / 10000;
                    a[j] %= 10000;
                    j--;
                }
                if(j <= t)//当前最高位进位了,则把t标志移到最高位的前一位
                    t = j - 1;
            }
        }
        for (int i = t + 1; i < 11; i++)//输出结果,注意可能会需要添加0的情况
        {
            if(i == t + 1)//最高位无需添加0
                cout << a[i];
            else
            {
                if(a[i] >= 1000)//占据四位需要添加0
                    cout << a[i];
                else if(a[i] >= 100)//不足四位
                    cout << "0" << a[i];
                else if(a[i] >= 10)
                    cout << "00" << a[i];
                else if(a[i] >= 1)
                    cout << "000" << a[i];
                else
                    cout << "0000";
            }
        }
        cout << endl;
    }
    return 0;
}
此算法采用的数组模拟“万进制”(逢万进一),即把结果保存在数组中,并模拟阶乘的求法:初始结果
 x 1 x 2 x 3......x n;最后输出的时候可能需要添加零的情况
发表于 2018-10-06 15:55:48 回复(1)
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)
#include<cstdio>
#include<iostream>

using namespace std;

long long  jiecheng(int n){
    if(n==0)return 1;
    else return(n*jiecheng(n-1));
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        cout<<jiecheng(n)<<endl;
    }
    return 0;
}

发表于 2021-03-12 22:19:48 回复(0)
一个高精度阶乘。
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e2;
int ans[maxn/2];
int len;

void getFact(int n)
{
    for(int x = 2;x <= n; x++)
    {
        int c = 0;
        for(int i = 0;i < len; i++)
        {
            int t = ans[i]*x+c;
            ans[i] = t%10; c = t/10;
        }
        while(c)
        {
            ans[len++] = c%10; c /= 10;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n)
    {
        fill(ans, ans+maxn/2, 0); len = 0;
        ans[len++] = 1;
        getFact(n);
        for(int i = len-1;i >= 0; i--)
            cout << ans[i];
        cout << "\n";
    }
    return 0;
}


发表于 2021-01-18 13:33:55 回复(0)
#include <stdio.h>

long long int count(long long int x);

int main(void)
{
    long long int n;
    
    while (scanf("%lld", &n) != EOF && (n >= 1 && n <= 20))
    {
        printf("%lld\n", count(n));
    }
    
    return 0;
}

long long int count(long long int x)
{
    if (1LL == x)
    {
        return 1LL;
    }
    else
    {
        return x * count(x - 1);
    }
}
//最保险的做法就是直接设为long long int进行递归,递归时最多压栈20层不会栈溢出;
发表于 2020-05-07 18:18:15 回复(0)
输入没有0就放心了
from functools import reduce
print(reduce(lambda x,y:x * y,range(1,1 + int(input()))))


编辑于 2020-03-23 12:26:39 回复(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)
本题n在20以内,用常规的递归方法就能过,但是只能算到30的阶乘。看了讨论区大佬的方法可以算到几千的阶乘,叹为观止。

#include<iostream>
using namespace std;
long long int factorial(int n){
	if(n==1){
		return 1;
	}
	else{
		return n*factorial(n-1);
	}
} 

int main(){
	int n;
	while(cin>>n){
		cout<<factorial(n)<<endl;
	}
	return 0;
}


发表于 2020-03-16 10:10:59 回复(0)
#include<iostream>
using namespace std;
long Result(long n)//练练递归
{
   return n==1?1:(n*Result(n-1));
}
int main()
{
    long n=0;
    while(cin>>n)
    {
        cout<<Result(n)<<endl;
    }
    return 0;
}

发表于 2020-03-11 18:12:35 回复(0)
只要MAXSIZE够大
10000阶乘都不怕
#include <stdio.h>
#define MAXSIZE 40000
void multiple(int *a,int m)
{
    int c=0;
    for(int i=MAXSIZE-1; i>=0; i--)
    {
        int t=a[i]*m+c;
        a[i]=t%10;
        c=t/10;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int a[MAXSIZE]= {0};
        a[MAXSIZE-1]=n;
        for(int i=n-1; i>1; i--)
        {
            multiple(a,i);
        }
        int flag=0;
        for(int i=0; i<MAXSIZE; i++)
        {
            if(a[i]!=0)
            {
                flag=1;
            }
            if(flag==1)
            {
                printf("%d",a[i]);
            }
        }
        printf("\n");
    }
    return 0;
}


编辑于 2020-03-01 23:38:38 回复(0)
规定n为1到20的数,用循环算阶乘,主要是注意要用long long 型
#include <iostream>
using namespace std;
int factorial(int n){
	long long  fac;
	fac = 1;
	for (int i = 1; i <= n; i++) {
		fac *= i;
	}
	cout << fac << endl;
	return 0;
}
int main() {
	int n;
	while (cin >> n) {
		factorial(n);
	}
}

发表于 2020-02-15 17:39:36 回复(0)
用的大数乘法加万进制,测试5000的阶乘没有问题,10000的阶乘也能算(很多位比较不了,不知道是不是正确的,理论上结果在40000位以内都能正常输出)。
#include <stdio.h>
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        int a[10000]={1};
        int p=0,c;                   //进位为c,用p指示当前结果位数
        for(int i=1;i<=n;i++)
        {
            c=0;
            for(int j=0;j<=p;j++)
            {
                a[j]=a[j]*i+c;          //a[j]此时实际的数值(乘积加上进位)
                c=a[j]/10000;              //下一位(a[j+1])的进位
                a[j]=a[j]%10000;           //a[j]要显示出的数值
            }
            while(c!=0)                 //若最终进位不为0,说明p+1上也有数值
            {
                a[++p]=c%10000;
                c/=10000;
            }
        }
        printf("%d",a[p]);               //第一位不需要输出前导0
        for(int i=p-1;i>=0;i--)printf("%04d",a[i]);       //逆序输出,注意前导0
        printf("\n");
    }
}
编辑于 2020-03-18 14:18:42 回复(0)

大数运算,存储一个大数的数组为1000大小时最大可以计算449的阶乘。代码丑陋。

#include<string>
using namespace std;

struct bign{
    int len;
    int d[1000];
    bign(){
        len = 0;
        fill(d, d + 1000, 0);
    }
};

bign multi(bign a, int b){
    bign c;
    int carry = 0;
    for(int i = 0; i < a.len; i++){
        int temp = a.d[i] * b + carry;
        c.d[c.len++] = temp % 10;
        carry = temp / 10;
    }
    while(carry != 0){
        c.d[c.len++] = carry % 10;
        carry /= 10;
    }
    return c;
}

void factorial(int n){
    bign ret;
    ret.len = 1;
    ret.d[0] = 1;
    for(int i = 2; i <= n; i++){
        ret = multi(ret, i);
    }
    cout << ret.len << endl;
    for(int i = ret.len - 1; i >= 0; i--){
        cout << ret.d[i];
    }
    cout << endl;
}

int main(){
    int n;
    while(cin >> n){
        bign ret;
        factorial(n);
    }
    return 0;
}
发表于 2019-03-02 21:13:21 回复(0)
 #include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    long result=1;
    for(int i=1;i<=n;i++){
        result*=i;
    }
    cout<<result<<endl;
} //感觉这个主要涉及的问题就是结果的存储类型吧,用long,由于限制了n的范围,所以还不需要用到大数乘法

发表于 2019-02-03 21:09:49 回复(0)
try:
    while True:
        num,result = int(input()),1
        while num > 1:
            result *= num
            num -= 1
        print(result)
except Exception:
    pass
编辑于 2018-10-09 23:52:51 回复(0)
#include <cstdio>

double fun(double n){
    if(n > 0)
        return n * fun(n - 1);
    else
        return 1;
}

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
        printf("%.0f\n", fun((double)n));
    return 0;
}

发表于 2018-02-15 17:13:18 回复(1)
#include<stdio.h>
#define MAX 300 
void fact(int n);
int main()
{
    int number;
    int p = scanf("%d",&number);
    while(p != EOF)
    {
        fact(number);
     	printf("\n");
        p = scanf("%d",&number);
        
    }
    return 0;  
 }
  
void fact(int n)
{
    int a[MAX]={1};
    int digit=1;    /*阶乘至少为1位,因此digit(位数)初始值为1*/ 
    int carry=0;    /*进位*/
    int temp,i,j;
    for (i=1;i<=n;i++) 
    { 
        for (j=0;j<digit;j++) 
        {
            temp=a[j]*i+carry;   /*temt为未进位前的数字*/ 
            a[j]=temp%10;
            carry=temp/10;
        }
        while(carry!=0) 
        {
            a[++digit-1]=carry%10; /*必须限制carry在[0,9]*/
            carry=carry/10;
        }   
   } 
    
   for(i=digit-1;i>=0;i--) 
    {
       printf("%d",a[i]);
    }
        
}
感觉好坑爹啊,原来这个OJ要一直输入测试用例!
发表于 2017-05-20 11:07:14 回复(0)
#include<stdio.h>
int main()
{
    int n;
    long long int p;
    while(scanf("%d",&n)!=EOF)
    {
        p=1;
        while(n>0)
        {
            p*=n;
            n--;
        }
        printf("%lld\n",p);
    }
    return 0;
}
发表于 2018-02-25 15:27:05 回复(0)
大数阶乘问题,但是题目做了简化,限制n(1<=n<=20),所以普通解法就能通过。
但是,如果不对n做限制呢?那就是典型的大数问题了,字符串是解决大数问题的有效手段,
可能不是最简单的,但是是万能的。对这个题来说,只要实现了字符串的乘法,不管多大的
n,都能实现阶乘的运算。例如a = "123",b = "56",a * b = "123" * "6" + "123" * 
"50",而"123" * "50" = "123" * "5" + "0". 这样就得到了两个大数相乘的结果。因
此,要实现字符串相乘运算,首先要实现字符串和单个字符的相乘运算(其实是为了简
化多位数乘法),然后要实现字符串的相加运算。这个思路实现如下:
#include <iostream>
#include <string>
using namespace std;

string add(const string a, const string b){
    string res;
    int i = a.size() - 1;
    int j = b.size() - 1;
    int extra = 0;
    while (i >= 0 && j >= 0){
        res = to_string((a[i] - '0' + b[j] - '0' + extra) % 10) + res;
        extra = (a[i] - '0' + b[j] - '0' + extra) / 10;
        i--;
        j--;
    }
    while (i >= 0){
        res = to_string((a[i] - '0' + extra) % 10) + res;
        extra = (a[i] - '0' + extra) / 10;
        i--;
    }
    while (j >= 0){
        res = to_string((b[j] - '0' + extra) % 10) + res;
        extra = (b[j] - '0' + extra) / 10;
        j--;
    }
    if (extra != 0)
        res = to_string(extra) + res;
    return res;
}

string _mul(const string a, const char b){
    string res;
    int extra = 0;
    int i = a.size() - 1;
    while (i >= 0){
        res = to_string(((a[i] - '0') * (b - '0') + extra) % 10) + res;
        extra = ((a[i] - '0') * (b - '0') + extra) / 10;
        i--;
    }
    if (extra != 0)
        res = to_string(extra) + res;
    return res;
}

string multiply(const string a, const string b){
    string res;
    for (int i = b.size() - 1; i >= 0; i--){
        string temp = _mul(a, b[i]);
        for (int j = 0; j < (int)b.length() - 1 - i; j++)
            temp = temp + "0";
        res = add(res, temp);
    }
    return res;
}

string factorial(int n){
    string res = "1";
    for (int i = 2; i <= n;i++){
        res = multiply(res, to_string(i));
    }
    return res;
}

int main(){
    int n;
    while (cin >> n){
        string res = factorial(n);
        cout << res << endl;
    }
    return 0;
}

编辑于 2018-01-17 17:07:50 回复(3)

问题信息

难度:
147条回答 15871浏览

热门推荐

通过挑战的用户

查看代码