每日一九度之 题目1076:N的阶乘

本文介绍了一种使用数组来处理大数乘法的方法,以此实现计算较大数值的阶乘。通过逐步增加乘法过程中的每一位数,有效解决了整数溢出的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间限制:3 秒

内存限制:128 兆

特殊判题:

提交:7601

解决:2749

题目描述:

 输入一个正整数N,输出N的阶乘。

输入:

正整数N(0<=N<=1000)

输出:

 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘

样例输入:
4
5
15
样例输出:
24
120
1307674368000

大数的乘法。

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#define INF 0x7fffffff
using namespace std;
const int maxn = 3005;
typedef long long ll;
int n;
ll a[maxn];

int main(){
    while( ~scanf("%d",&n) ){
         if( n == 0 ){
             printf("1\n");
             continue;
         }
         //进位 
         int c = 0, len = 1;
         memset(a,0,sizeof(a));
         a[0] = 1;
         for(int i=1; i<=n; i++){
             c = 0;
             for(int j=0; j<len; j++){
                 a[j] = a[j] * i + c;
                 if( a[j]>=10 ){
                     c = a[j] / 10;
                     a[j] = a[j] % 10;
                 } else c = 0;
             }
             while( c != 0 ){
                 int t = c % 10;
                 a[len++] = t;
                 c = c / 10;
             }
         }
         int i, j;
         for(i=maxn; i>=0; i--){
             if( a[i] != 0 ){
                 break;
             }
         }
         for(j=i; j>=0; j--){
             printf("%d",a[j]);
         }
         printf("\n");
    } 
    return 0;
}

 

转载于:https://www.cnblogs.com/Asimple/p/5932768.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值