• 牛客网 2018年全国多校算法寒假训练营练习比赛(第三场)E.进击吧!阶乘-Java大数


     
    E.进击吧!阶乘
     
    时间限制:C/C++ 3秒,其他语言6秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    给定一个整数N(0≤N≤10000),求取N的阶乘

    输入描述:

    多个测试数据,每个测试数据输入一个数N

    输出描述:

    每组用一行输出N的阶乘
    示例1

    输入

    1
    2
    3

    输出

    1
    2
    6



    最近一堆大数的,头大。

    代码:Java

     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3  
     4 public class Main {
     5     public static void main(String[]args) {
     6         Scanner cin=new Scanner(System.in);
     7         int n;
     8         while(cin.hasNext()) {
     9             BigInteger ans,temp;
    10             n=cin.nextInt();
    11             ans=BigInteger.valueOf(1);
    12             if(n==0) {
    13                 System.out.println(1);
    14             }
    15             else {
    16                 for(int i=1;i<=n;i++) {
    17                     temp=BigInteger.valueOf(i);
    18                     ans=ans.multiply(temp);
    19                 }
    20                 System.out.println(ans);
    21             }
    22         }
    23     }
    24 }
    
    
    

    还有一个不是Java的,不是我写的

    代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<stack>
     7 #include<map>
     8 #include<vector>
     9 #include<queue>
    10 #include<set>
    11 using namespace std;
    12 const int inf=1<<30;
    13 const int maxn=1e5+10;
    14 const double eps=1e-6;
    15 const int mod=1e9+7;
    16 typedef long long ll;
    17 int a[maxn];
    18 int main(){
    19     int n;
    20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    21     while(cin>>n){
    22         memset(a,0,sizeof(a));
    23         a[0]=1;
    24         int h=1;
    25         for(int i=1;i<=n;i++){
    26             int res=0;
    27             for(int j=0;j<h;j++){
    28                 int temp=a[j]*i+res;
    29                 a[j]=temp%10;
    30                 res=temp/10;
    31             }
    32             while(res){
    33                 a[h++]=res%10;
    34                 res/=10;
    35             }
    36         }
    37         for(int k=h-1;k>=0;k--)
    38             cout<<a[k];
    39         cout<<endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    yii---where or该如何使用
    yii---获取当前sql语句
    yii---load怎么使用
    yii---往对象里面添加属性
    js---箭头函数
    yii---判断POST请求
    Atitit USRqc62204 证书管理器标准化规范
    Atitit  深入理解命名空间namespace  java c# php js
    atitit..代码生成流程图 流程图绘制解决方案 java  c#.net  php v2
    Atitit 项目中的勋章体系,,mvp建设 ,荣典体系建设
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9711217.html
Copyright © 2020-2023  润新知