求n(10000以内)的阶乘

题目地址
http://noi.openjudge.cn/ch0106/14/
把结果分开存储,存到数组或容器中

#include
#include
#include
#include
using namespace std;

#include
void cheng(vector<unsigned long long int> &result, int t)
{
    int temp=0;
    for (auto &x : result)
    {
        unsigned long long int q = (x*t + temp) /1000000000000000;
        x = (x*t+temp) % 1000000000000000;
        temp = q;
    }
    if (temp != 0) result.push_back(temp);
}
int main()
{
    std::ios::sync_with_stdio(false);
//  int a = clock();
    vector<unsigned long long int> result;
    result.push_back(1);
        result.clear();
        result.push_back(1);
    int N;cin >> N;
    for (int t = 2;t <= N;t++)
        cheng(result, t);
    cout << result.back();
    for (auto it = result.rbegin() + 1;it != result.rend();it++)
        cout << setw(15) << setfill('0') << *it;
    cout << endl;
//  int b = clock();
//  cout << b - a << endl;
}

你可能感兴趣的:(求n(10000以内)的阶乘)