C++高精度加乘类模板——POJ1503 Integer Inquiry

竟然发现这个题还没发,那我就发了吧。

POJ_1503: Integer Inquiry
Time Limit: 1000 MS Memory Limit: 10 MB 64bit IO Format: %I64d
Submitted: 8 Accepted: 3
[Submit][Status][Web Board]
Description
One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
This supercomputer is great,'' remarked Chip.I only wish Timothy were here to see these results.’’ (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)
Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output
370370367037037036703703703670

加法模拟可跳转友情链接 https://blog.csdn.net/weixin_43269437/article/details/103739220
源代码C++

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 1e3 + 5;
class Integer {
    
    
public:
    int num[maxn];
    int len;
 
    Integer();
    Integer(const Integer &t);
    Integer(const string &str);
    Integer& operator = (const Integer &t);
};
 
Integer::Integer(const Integer &t) {
    
    
    memset(num, 0 ,sizeof(num));
    for(int i = 0; i < t.len; i++)
        num[i] = t.num[i];
    len = t.len;
}
 
Integer& Integer::operator = (const Integer &t) {
    
    
    memset(num, 0 ,sizeof(num));
    for(int i = 0; i < t.len; i++)
        num[i] = t.num[i];
    len = t.len;
 
    return *this;
}
 
ostream& operator << (ostream& o, Integer &in) {
    
    
    for(int i = in.len - 1; i >= 0; i--)
        o << in.num[i];
    o << endl;
 
    return o;
}
 
Integer::Integer() {
    
    
    memset(num, 0, sizeof(num));
    len = 0;
}
 
Integer::Integer(const string &str) {
    
    
    memset(num, 0 ,sizeof(num));
    this->len = str.size();
    for(int i = 0; i < this->len; i++) {
    
    
        this->num[i] = str[this->len - 1 - i] - '0';
    }
}
 
Integer operator + (const Integer &num1, const Integer &num2){
    
    
    Integer ans;
    ans.len = (num1.len > num2.len? num1.len: num2.len);
 
    for(int i = 0; i < ans.len; i++) {
    
    
        ans.num[i + 1] = (ans.num[i] + num1.num[i] + num2.num[i]) / 10;
        ans.num[i] = (ans.num[i] + num1.num[i] + num2.num[i]) % 10;
    }
 
    if(ans.num[ans.len])
        ans.len++;
    return ans;
}
 
int main() {
    
    
    Integer ans("0");
    string str;
    while(cin >> str && str != "0") {
    
    
        Integer temp(str);
        ans = ans + temp;
    }
    cout << ans;
    return 0;
}

附高精模板
虽然队友一直让我学啥FFT,但我不会啊~

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 5; //注意栈空间溢出

class bigint {
    
    
public:
    int a[maxn];
    int len;

    bigint();
    bigint(const char* t);
    bigint operator * (const bigint& t) const;
    bigint operator + (const bigint &t) const;
    void display();
    bigint& operator = (const bigint &t);
    bool operator == (const bigint &t) const;
    bool operator < (const bigint &t) const;
};

ostream& operator << (ostream& o, const bigint &t) {
    
    
    for(int i = t.len - 1; i >= 0; i--)
        cout << t.a[i];

    return o;
}

bool bigint::operator < (const bigint &t) const {
    
    
    if(len != t.len) return len < t.len;

    for(int i = len - 1; i >= 0; i--) {
    
    
        if(a[i] != t.a[i])
            return a[i] < t.a[i];
    }

    return false;
}

bigint::bigint() {
    
    
    len = 0;
    memset(a, 0, sizeof(a));
}

bool bigint::operator == (const bigint &t) const{
    
    
    if(len != t.len) return false;
    for(int i = len - 1; i >= 0; i--)
        if(t.a[i] != a[i]) return false;
    return true;
}

bigint& bigint::operator = (const bigint &t) {
    
    
    memset(a, 0, sizeof(a));
    for(int i = 0; i < t.len; i++)
        a[i] = t.a[i];
    len = t.len;

    return *this;
}

bigint::bigint(const char* t) {
    
    
    len = strlen(t);
    memset(a, 0, sizeof(a));
    for(int i = len - 1; i >= 0; i--) {
    
    
        a[i] = t[len - i - 1] - '0';
    }
}

bigint bigint::operator * (const bigint& t) const{
    
    
    bigint ans;
    ans.len = t.len + len - 1;

    for(int i = 0; i < len; i++) {
    
    
        for(int j = 0; j < t.len; j++) {
    
    
            ans.a[i + j + 1] += (ans.a[i + j] + a[i] * t.a[j]) / 10;
            ans.a[i + j] = (ans.a[i + j] + a[i] * t.a[j]) % 10;
        }
    }

    while(ans.a[ans.len]) {
    
    
        ans.a[ans.len + 1] += ans.a[ans.len] / 10;
        ans.a[ans.len] %= 10;
        ans.len++;
    }

    return ans;
}

bigint bigint::operator + (const bigint &t) const{
    
    
    bigint ans;
    ans.len = (len > t.len? len: t.len);

    for(int i = 0; i < ans.len; i++) {
    
    
        ans.a[i + 1] = (ans.a[i] + a[i] + t.a[i]) / 10;
        ans.a[i] = (ans.a[i] + a[i] + t.a[i]) % 10;
    }

    if(ans.a[ans.len]) ans.len++;

    return ans;
}

 int main() {
    
    
    char a[10000], b[10000];

    while(cin >> a >> b) {
    
     
        bigint num1(a), num2(b); //注意栈空间溢出 重载构造函数
        cout << num1 + num2 << "\t" << num1 * num2 << endl; //重载+ * <<  拷贝构造函数
        bigint a1("99999999999999999999999999"), a2("8888888888888888888888"), a3("7777777777777777"), a4("0");
        bigint num[4];
        num[0] = a1, num[1] = a2, num[2] = a3, num[3] = a4;//重载=
        sort(num, num + 4);//重载 <
        for(int i = 0; i < 4; i++)
            cout << num[i] << " ";
        cout << endl;
    }

    return 0;
 }


猜你喜欢

转载自blog.csdn.net/weixin_43269437/article/details/103739996
今日推荐