模板 int128

__int128范围大约在\(-1.7\times10^{37}\) ~ \(1.7\times10^{37}\)之间,unsigned __int128 的范围大约在 \(0\) ~ \(3.4\times10^{38}\)之间。

__int128仅支持四则运算,输入输出要自已写。

输入

void read(i128 &n) {
    i128 x = 0; bool f = false;
    char ch = cin.get();
    while(!isdigit(ch)) {
        if(ch == '-') f = true;
        cin.get(ch);
    }
    while(isdigit(ch)) {
        x = x * 10 + ch - '0';
        cin.get(ch);
    }
    n = f ? -x : x;
}

输出

void print(i128 n) {
    if(n < 0) {
        cout.put('-');
        n *= -1;
    }
    if(n > 9) {
        print(n / 10);
    }
    cout.put(n % 10 + '0');
}

赋值

如果要付一个超过long long的值,用以下方法。

i128 to_int128(string s) {
    i128 m = 0;
    bool f = false;
    for(int i = 0; i < s.size(); i++) {
        if(i == 0 && s[i] == '-') {
            f = true;
            continue;
        }
        m *= 10;
        m += s[i] - 48;
    }
    return f ? -m : m;
}

posted @ 2023-07-30 01:28  wuyoudexian  阅读(74)  评论(0)    收藏  举报