读入优化,减小常数!

读入优化虽好,千万不要写挂!

一、保证数据非负

写法1

int read()
{
	register char ch=getchar();register int in=0;
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') in=(in<<3)+(in<<1)+ch-'0',ch=getchar();
	return in;
}

写法2

int read()
{
	register char ch;
	while(ch=getchar(),ch<'0'||ch>'9');
	register int in=ch-'0';
	while(ch=getchar(),ch>='0'&&ch<='9') in=(in<<3)+(in<<1)+ch-'0';
	return in;
}

二、数据含有负数

写法1

int read()
{
    register char ch;
    while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
    register bool neg=ch=='-';register int in=neg?0:ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9') in=(in<<3)+(in<<1)+ch-'0';
    return neg?-in:in;
}

写法2

int read()
{
    register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    register bool neg=ch=='-';register int in=neg?0:ch-'0';ch=getchar();
    while(ch>='0'&&ch<='9') in=(in<<3)+(in<<1)+ch-'0',ch=getchar();
    return neg?-in:in;
}

猜你喜欢

转载自blog.csdn.net/pig_dog_baby/article/details/81131707