众所周知,c++中有一种类型,叫做__int128,它可以存储 的整数。
但是,c++的标准输入输出中并不支持__int128类型的读写,这对于喜欢使用cin和cout读写的人十分不友好,于是,我们想到可以通过重载左移和右移运算符的方式实现读写。
思路如下:
-
对于输入,我们使用快读,再定义一个标准输入类型的运算符,返回所使用的输入流。(注意读入的变量不要忘记加取址符
&) -
对于输出,先特判
x=0的情况,直接输出0,不要忘记返回值。再判断x是否为负数,如果是,直接输出负号-,然后将x变为-x。接着定义字符串s,每次取个位上的值,然后将其放到s的首项,最后输出s并返回。
代码如下:
#include <iostream>
#include <cstdint>
#include <string>
using namespace std;
__int128 read()
{
__int128 ans=0;
short sign=1;
char c=getchar();
while(!(c>='0'&&c<='9'))
{
if(c=='-')
{
sign=-1;
}
c=getchar();
}
while(c>='0'&&c<='9')
{
ans*=10;
ans+=c-'0';
c=getchar();
}
return ans*sign;
}
istream& operator>>(istream& is,__int128& x)
{
x=read();
return is;
}
ostream& operator<<(ostream& os,__int128 x)
{
if(x==0)
{
return os << 0;
}
if(x<0)
{
putchar('-');
x=-x;
}
string s="";
while(x)
{
s=char(x%10+'0')+s;
x/=10;
}
return os << s;
}
__int128 x;
int main()
{
cin >> x;
cout << x;
return 0;
}