首页 > 学院 > 开发设计 > 正文

求n(10000以内)的阶乘

2019-11-11 01:53:27
字体:
来源:转载
供稿:网友

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

#include<iostream>#include<string>#include<time.h>#include<iomanip>using namespace std;#include<vector>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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表