CodeChef - Small factorials

来源:互联网 发布:尔湾 知乎 编辑:程序博客网 时间:2024/06/03 22:02

题目:http://www.codechef.com/problems/FCTRL2/

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
分析:

位数较长,可使用vector来处理。

代码:

#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;}

0 0
原创粉丝点击