快速读入的方法

快速读入是一种应对卡常和提高暴力算法的AC量的算法。它的原理是读入一个char类型的字符要快于读入一个int类型的数字。它可以满足一次性读入一个数字的操作。

下面是快速读入函数read()的代码:

 

 1 inline int read(){
 2     int x=0,f=1;char ch=getchar();
 3     while(!isdigit(ch)){
 4         if(ch=='-'){
 5             f=-1;
 6             ch=getchar();
 7         }
 8     } 
 9     while(isdigit(ch)){
10         x=x*10+ch-48;
11         ch=getchar();
12     }
13     return x*f;
14 }

 

 

 

在读入中使用该函数时,只需要对其进行调用即可。例如,需要读入一个int类型的数,只需要将"cin>>n"替换为"n=read()"即可。

 

posted on 2020-07-05 15:47  郭谦  阅读(956)  评论(1编辑  收藏  举报