__int128的相关操作

传送门:点击打开链接

题意:四个数相加。

分析:可以用java大数来做,也可以使用_int128来做,或者用unsigned long long (最大为2^64-1)对四个数都为2^62特判一下就好了。写这篇博客主要了解_int128的用法。

补充:实际上,gcc是不支持__int128这种数据类型的,比如在codeblocks 16.01也是无法编译的,但是提交到大部分OJ上是可以编译且能用的。C/C++标准IO是不认识__int128这种数据类型的,因此要自己实现IO,其他的运算,与int没有什么不同。 

代码:(g++)

#include  
using namespace std;  
typedef unsigned long long ll;  
void print(__int128 x)  
{  
    if(!x)  
    {  
        puts("0");  
        return ;  
    }  
    string ret="";  
    while(x)  
    {  
        ret+=x%10+'0';  
        x/=10;  
    }  
    reverse(ret.begin(),ret.end());  
    cout<

 

写个了__int128版本的a+b,代码如下:

#include 
using namespace std;

void scan(__int128 &x)//输入
{
    x = 0;
    int f = 1;
    char ch;
    if((ch = getchar()) == '-') f = -f;
    else x = x*10 + ch-'0';
    while((ch = getchar()) >= '0' && ch <= '9')
        x = x*10 + ch-'0';
    x *= f;
}
void _print(__int128 x)
{
    if(x > 9) _print(x/10);
    putchar(x%10 + '0');
}
void print(__int128 x)//输出
{
    if(x < 0)
    {
        x = -x;
        putchar('-');
    }
    _print(x);
}

int main()
{
    __int128 a, b;
    scan(a); scan(b);
    print(a + b);
    return 0;
}

 

你可能感兴趣的:(_int128)