题解 | #N的阶乘#

N的阶乘

https://www.nowcoder.com/practice/f54d8e6de61e4efb8cce3eebfd0e0daa

#include<iostream>
#include<algorithm>

std::string MultiLongInt(const std::string& s1, const std::string& s2)
{
    std::string result(s1.size() + s2.size(), '0');

    for (int i = s1.size() - 1; i >= 0; i--) {
        int carry = 0;
        for (int j = s2.size() - 1; j >= 0; j--) {
            int mul = (s1[i] - '0') * (s2[j] - '0') + (result[i + j + 1] - '0') + carry;
            carry = mul / 10;
            result[i + j + 1] = mul % 10 + '0';
        }
        result[i] += carry;
    }

    size_t startpos = result.find_first_not_of("0");
    if(startpos == std::string::npos) return "0";

    return result.substr(startpos);
}

int main()
{
    int n;
    std::string sum;
    while (std::cin >> n)
    {
        sum = '1';
        for(int i=2;i<=n;i++)
        {
            sum = MultiLongInt(sum, std::to_string(i));
        }
        std::cout << sum << std::endl;
    }
    
}

全部评论

相关推荐

05-12 22:16
已编辑
北京邮电大学 研发工程师
牛客30236098...:0offer+1 滴滴都不给我面 佬没投鹅吗,鹅应该很喜欢北邮吧
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务