题解 | #N的阶乘#

N的阶乘

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

#include <iostream>
#include <cstring>

using namespace std;

struct bign {
    int d[100000];
    int len;

    bign() {
        len = 0;
        memset(d, 0, sizeof(d));
    }

};

bign add(bign a, bign b) {
    bign c;
    int carry = 0;//进位
    for (int i = 0; i < a.len || i < b.len; i++) {
        int temp = a.d[i] + b.d[i] + carry;
        c.d[c.len++] = temp % 10;
        carry = temp / 10;
    }
    if (carry != 0) {
        c.d[c.len++] = carry;
    }
    return c;
}

bign multi(bign a, int b) {
    bign c;
    int carry = 0;
    for (int i = 0; i < a.len; i++) {
        int temp = a.d[i] * b + carry;
        c.d[c.len++] = temp % 10;
        carry = temp / 10;
    }
    while (carry != 0) {
        c.d[c.len++] = carry % 10;
        carry = carry / 10;
    }
    return c;
}

bign change(char s[]) {
    bign c;
    string str = s;
    c.len = str.size();
    for (int i = 0; i < c.len; i++) {  //倒着赋值
        c.d[i] = str[c.len - i - 1] - '0';
    }
    return c;
}

void print(bign a) {
    for (int i = a.len - 1; i >= 0; i--) {
        printf("%d", a.d[i]);
    }
    printf("\n");
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        bign c;
        c.len = 1;
        c.d[0] = 1;
        for (int i = 1; i <= n; i++) {
            c = multi(c, i);
        }
        print(c);
    }
    return 0;
}

全部评论

相关推荐

投递大华股份等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务