1.最基本的
int read()
{
int x = 0, f = 1; char ch = getchar();
while (!isdigit (ch))
{
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit (ch))
{
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
2.看起来高级一点的(跟第一个差不了多少)
template < typename T >
void read (T &x)
{
x = 0; int f = 1; char ch = getchar();
while (!isdigit (ch))
{
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit (ch))
{
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
3.二进制运算的
template < typename T >
void read (T &x)
{
x = 0; int f = 1; char ch = getchar();
while (!isdigit (ch))
{
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit (ch))
{
x = (x << 1) + (x << 3) + (ch & 15);
ch = getchar();
}
x *= f;
}
4.减少几次判断的(好用也好打)
template < typename T >
void read (T &x)
{
x = 0; bool f = 0; char ch = getchar();
while (!isdigit (ch)) f ^= !(ch ^ 45), ch = getchar();
while (isdigit (ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
x = f ? -x : x;
}
5.在我认知中跑得最快的
char buf[1 << 21], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++)
template < typename T >
void read (T &x)
{
x = 0; bool f = 0; char ch = getchar();
while (!isdigit (ch)) f ^= !(ch ^ 45), ch = getchar();
while (isdigit (ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
x = f ? -x : x;
}