OpenJudge

发个答案

  • Altair(RI)
    Altair(RI) 17.9.24 回复

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    const int MAX = 110;
    class CHugeInt {
    // 在此处补充你的代码
    string s;
    friend CHugeInt f(const CHugeInt &a, const CHugeInt &b){
    int ia = a.s.length() - 1;
    int ib = b.s.length() - 1;
    int t = 0; //同位相加的和
    string s;
    int fw = 0; //进位标识
    do{
    char ca, cb;
    ca = ia < 0?'0' : a.s[ia];
    cb = ib < 0?'0' : b.s[ib];
    t = ca + cb - '0' - '0';
    s.append(1, (t+fw) % 10 + '0');
    fw = (t+fw) > 9?1 : 0;
    if (ia >= 0)
    ia --;
    if (ib >= 0)
    ib --;
    } while(ia >= 0 || ib >= 0);
    if (fw){
    s.append(1,'1');
    }
    string s2;
    for (int i = s.length() - 1; i >= 0; i --){
    s2.append(1, s[i]);
    }
    return CHugeInt(s2);
    }
    public:
    CHugeInt(string t): s(t){ };
    CHugeInt(char * c): s(c){ };
    CHugeInt(int t){
    while (t != 0){
    s.insert(0, 1, '0' + t % 10);
    t = t / 10;
    }
    };
    friend ostream &operator << (ostream &output, CHugeInt a){
    output << a.s;
    return output;
    }
    friend CHugeInt operator +(CHugeInt a, CHugeInt b){
    return f(a, b);
    }
    void operator +=(int n){
    *this = f(*this, n);
    }
    CHugeInt &operator++(){
    *this += 1;
    return *this;
    }
    CHugeInt operator++(int){
    CHugeInt t = *this;
    *this += 1;
    return t;
    }
    };
    int main()
    {
    char s[210];
    int n;

    while (cin >> s >> n) {
    CHugeInt a(s);
    CHugeInt b(n);
    // cout << a << endl;
    cout << a + b << endl;
    cout << n + a << endl;
    cout << a + n << endl;
    b += n;
    cout << ++ b << endl;
    cout << b++ << endl;
    cout << b << endl;
    }
    return 0;
    }

  • [FF000]内涵段子躺枪
    [FF000]内涵段子躺枪 17.11.7 回复

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    const int MAX = 110;
    class CHugeInt {
    // 在此处补充你的代码
    string s;
    friend CHugeInt f(const CHugeInt &a, const CHugeInt &b){
    int ia = a.s.length() - 1;
    int ib = b.s.length() - 1;
    int t = 0; //同位相加的和
    string s;
    int fw = 0; //进位标识
    do{
    char ca, cb;
    ca = ia < 0?'0' : a.s[ia];
    cb = ib < 0?'0' : b.s[ib];
    t = ca + cb - '0' - '0';
    s.append(1, (t+fw) % 10 + '0');
    fw = (t+fw) > 9?1 : 0;
    if (ia >= 0)
    ia --;
    if (ib >= 0)
    ib --;
    } while(ia >= 0 || ib >= 0);
    if (fw){
    s.append(1,'1');
    }
    string s2;
    for (int i = s.length() - 1; i >= 0; i --){
    s2.append(1, s[i]);
    }
    return CHugeInt(s2);
    }
    public:
    CHugeInt(string t): s(t){ };
    CHugeInt(char * c): s(c){ };
    CHugeInt(int t){
    while (t != 0){
    s.insert(0, 1, '0' + t % 10);
    t = t / 10;
    }
    };
    friend ostream &operator << (ostream &output, CHugeInt a){
    output << a.s;
    return output;
    }
    friend CHugeInt operator +(CHugeInt a, CHugeInt b){
    return f(a, b);
    }
    void operator +=(int n){
    *this = f(*this, n);
    }
    CHugeInt &operator++(){
    *this += 1;
    return *this;
    }
    CHugeInt operator++(int){
    CHugeInt t = *this;
    *this += 1;
    return t;
    }
    };
    int main()
    {
    char s[210];
    int n;

    while (cin >> s >> n) {
    CHugeInt a(s);
    CHugeInt b(n);
    // cout << a << endl;
    cout << a + b << endl;
    cout << n + a << endl;
    cout << a + n << endl;
    b += n;
    cout << ++ b << endl;
    cout << b++ << endl;
    cout << b << endl;
    }
    return 0;
    }

想要评论吗?

注册OpenJudge账号,如果您已经注册,请先登入