题解 | #N的阶乘#

N的阶乘

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

#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int MAXN = 10000;
struct BigInteger {
    int digital[MAXN];
    int length;
    BigInteger();
    BigInteger(int x);
    BigInteger operator=(int x);
    BigInteger operator=(const BigInteger& b);
    BigInteger operator*(const BigInteger& b);
    friend ostream& operator<<(ostream& out, const BigInteger& b);
    friend istream& operator>>(istream& in, BigInteger& b);

};
BigInteger BigInteger::operator*(const BigInteger& b) {
    BigInteger answer;
    answer.length = length + b.length;
//    cout<<length;
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < b.length; j++) {
//                cout<<digital[i]<<b.digital[j];
            answer.digital[i + j] += digital[i] * b.digital[j];
        }
    }
    for (int k = 0; k < answer.length; k++) {
        int x = answer.digital[k];
        answer.digital[k] = x % 10;
//        cout<<answer.digital[k];
        answer.digital[k + 1] += x / 10;
//    cout<<answer.digital[k+1];
    }
    while (answer.digital[answer.length - 1] == 0 && answer.length > 1) {
        answer.length--;
//        cout<<answer.length;
    }
    return answer;
}
istream& operator>>(istream& in, BigInteger& b) {
    int x;
    in >> x;
    b = x;
    return in;
}
BigInteger BigInteger::operator=(int x) {
    memset(digital, 0, sizeof(digital));
    length = 0;
    if (x == 0) {
        length = 1;
        digital[0] = 0;
    }
    while (x) {
        digital[length++] = x % 10;
        x /= 10;

    }
    return* this;
}
BigInteger BigInteger::operator=(const BigInteger& b) {
    memset(digital, 0, sizeof(digital));
    length = 0;
    for (int i = 0; i < b.length; i++) {
        digital[length++] = b.digital[i];
    }
    return* this;
}
BigInteger::BigInteger() {
    memset(digital, 0, sizeof(digital));
    length = 0;
}
BigInteger::BigInteger(int x) {
    memset(digital, 0, sizeof(digital));
    length = 0;
    while (x) {
        digital[length++] = x % 10;
        x /= 10;
    }
}
ostream& operator<<(ostream& out, const BigInteger& b) {
    for (int i = b.length - 1; i >= 0; i--) {
        out << b.digital[i];
    }
    return out;
}
int main() {
    BigInteger opera;
    int b;
    while (cin >> b) {
        opera = b;
//        cout<<opera;
//        cout<<BigInteger(b);
        BigInteger RES(1);
        for (int k = 1; k <= b; k++) {
            RES = RES * BigInteger(k);

        }
        cout << RES << "\n";
    }
    return 0;
}

全部评论

相关推荐

冷艳的小师弟在看机会:jd测评乱点直接被挂了,哭死~
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务