1 条题解

  • 0
    @ 2025-4-12 21:47:19

    C :

    #include<stdio.h>
    int main()
    {
            int n,i;
            double sum;
            while(scanf("%d",&n)!=EOF)
            {
                    sum=1.0;
                    for(i=1;i<=n;i++)
                    {
                            sum=sum*i;
                    }
                    printf("%.0lf\n",sum);
            }
            return 0;
    }
    

    C++ :

    #include <stdio.h>
    #include <string.h>
    
    typedef struct{
    	int nums[3300];
    	int length;
    }BigInteger;
    
    BigInteger Create(int num){
    	BigInteger ans;
    	ans.length = 0;
    	while(num){
    		ans.nums[ans.length++] = num%10;
    		num /= 10;
    	}
    	return ans;
    }
    
    BigInteger Multiply(BigInteger num1, BigInteger num2){
    	BigInteger ans;
    	memset(ans.nums, 0, sizeof(ans.nums));
    	ans.length = 0;
    	for(int i=0; i<num1.length; i++){
    		for(int j=0; j<num2.length; j++){
    			ans.nums[i+j] += num1.nums[i]*num2.nums[j];
    		}
    	}
    	while(ans.length<num1.length+num2.length-1 || ans.nums[ans.length]){
    		ans.nums[ans.length+1] += ans.nums[ans.length]/10;
    		ans.nums[ans.length] %= 10;
    		ans.length++;
    	}
    	return ans;
    }
    
    void Print(BigInteger num){
    	if(num.length == 0){
    		printf("0");
    	}
    	int i = num.length;
    	while(i--){
    		printf("%d", num.nums[i]);
    	}
    }
    
    BigInteger ans[1100];
    
    int main(){
    	int n;
    	ans[0] = Create(1);
    	ans[1] = Create(1);
    	for(int i=2; i<1100; i++){
    		ans[i] = Multiply(ans[i-1], Create(i));
    	}
    	while(scanf("%d", &n) != EOF){
    		Print(ans[n]);
    		putchar('\n');
    	}
    
    	return 0;
    }
    
    

    Pascal :

    var
      n,i:longint;
      s:int64;
    begin
        while not(eof) do
          begin
            s:=0;
            readln(n);
            s:=1;
            for i:=1 to n do
              s:=s*i;
            writeln(s);
          end;
    end.
    

    Java :

    import java.util.*; 
    import java.math.BigInteger; 
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner cin = new Scanner(System.in);
    		while(cin.hasNext()){
    			int n = cin.nextInt();
    			
    			BigInteger ans = BigInteger.ONE;
    			for(int i=1;i<=n;i++){
    				ans = ans.multiply(BigInteger.valueOf(i));
    			}
    			System.out.println(ans);
    		}
    	}
    }
    
    

    Python :

    import sys,math
    
    for line in sys.stdin:
        a = int(line.split()[0])
        print math.factorial(a)
    

    C# :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace n的阶乘
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s="";
                for (; ; )
                {
                    s = Console.ReadLine();
                    if(s==null)
                        break;
                    Console.WriteLine("{0}", xxx(int.Parse(s)));
                }
            }
            public static long xxx(int n)
            {
                if (n > 1)
                    return n * xxx(n - 1);
                else
                    return 1;
            }
        }
    }
    
    
    • 1

    信息

    ID
    1719
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者