蓝桥杯:试题 基础练习 阶乘计算

试题 基础练习 阶乘计算

没啥难度,模拟一下乘法运算就行。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Talk is cheap . Show me the code.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    vector<int> ans;
    ans.push_back(1);
    int flag=0,n=0,temp=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        int j=0;
        do{
            if(j>=ans.size()) ans.push_back(0);
            temp=i*ans[j]+flag;
            ans[j]=temp%10;
            flag=temp/10;
            j++;
        }while(j<ans.size()||flag);
    }
    reverse(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++)
    cout<<ans[i];
    return 0;
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄