打印100的阶乘(大数问题,Java好解)

来源:互联网 发布:网络暴力英文ppt 编辑:程序博客网 时间:2024/06/08 10:32

FCTRL2 - Small factorials

#math#big-numbers

You are asked to calculate factorials of some small positive integers.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

Output

For each integer n given at input, display a line with the value of n!

Example

Sample input:
41253

Sample output:

121206
    #include <iostream>      #include <vector>            std::vector<int> v;            void fact(int n)      {          v.clear();  // !          v.resize(200, 0);          v[199] = 1;                for(int i = 2; i <= n; ++i)          {              int carry = 0;              for(int j = 199; j >= 0; --j)              {                  v[j] = v[j] * i + carry;                  carry = v[j] / 10;                  v[j] %= 10;              }          }      }            int main()      {          int T;          std::cin >> T;          int n;                while(T--)          {              std::cin >> n;                    fact(n);                    int i = 0;              while(v[i] == 0)              {                  ++i;              }              for(; i < 200; ++i)              {                  std::cout << v[i];              }              std::cout << std::endl;          }                return 0;      }  
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.math.BigInteger;/****************************************************************************************************************/public class Main {    public static void main(String[] args)throws IOException{        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));        BigInteger[] fac = new BigInteger[101];        fac[0] = fac[1] = BigInteger.ONE;                for(int i = 2;i <= 100;i++)        {            BigInteger temp = BigInteger.valueOf(i);            fac[i] = fac[i-1].multiply(temp);        }                int t = Integer.parseInt(bf.readLine());        while(t-- > 0){            int num = Integer.parseInt(bf.readLine());            System.out.println(fac[num]);        }    }}




原创粉丝点击