C++卡常

1.读入优化(*)

低阶版:

inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())
		if(ch=='-') f=-1;
	for(;ch>='0'&&ch<='9';x=(x<<3)+(x<<1)+(ch^48),ch=getchar());
	return x*f;
}

 

高阶版:

inline char getc()
{
	static char buf[1<<20],*fs,*ft;
	return(fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin)),fs==ft)?EOF:*fs++;
}
inline void read(int &x)
{
	char ch=getc();
	for(x=0;ch=='\n'||ch==' ';ch=getc());
	for(;ch!='\n'&&ch!=' '&&ch!=EOF;ch=getc())
		x=((x+(x<<2))<<1)+(ch^0x30);
}
inline void read_str(char *str)
{
	char ch=getc();
	for(;ch<'0'||ch>'9';ch=getc());
	for(;ch<'0'||ch>'9';ch=getc())
		*str=ch,str++;
	*str='\0';
}

 

 

 

2.输出优化

低阶版:

void print(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}

 

高阶版:

static const int BUF=50000000;
char buf[BUF],*h=buf;
inline void put(char ch)
{
    h==buf+BUF?(fwrite(buf,1,BUF,stdout),h=buf):0;
    *h++=ch;
}
inline void putint(int num)
{
    static char _buf[30];
    sprintf(_buf,"%d",num);
    for(char *s=_buf;*s;s++)put(*s);
}
inline void finish()
{
    fwrite(buf,1,h-buf,stdout);
}

注意:put输出字符类型的,putint输出整数类型的。并且在主程序结束前打上finish();

 

 

 

3.register

举个例子for(int i=1;i<=n;i++)   =>   for(register int i=1;i<=n;i++)

 

 

4.inline

举个例子int add(int x,int y){return x+y;}   =>   inline int add(int x,int y){return x+y;}

 

 

5.位运算(*)

举个例子x*=2   =>   x<<=1

 

 

 

6.减少使用STL(*)

STL是一个常数非常大的东西。

 

 

7.用上define

define比赋值要快!!!

 

 

只要你的“暴力”不是很“暴力”,TLE1~2个点时,应该够用了。

带*号的真的很有用!!!

最后,感谢LJY大佬对我卡常知识的指导!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值