题解 | #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;
}

全部评论

相关推荐

11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
喜欢吃蛋糕仰泳鲈鱼是我的神:字节可以找个hr 给你挂了,再放池子捞
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务