顾名思义,快速读入。
如有不妥,请指正
你需要了解:
- inline内联函数:免去调用,相当于直接在函数处,效率高
- 没了
上代码
#include<iostream>
#include<cstring>
using namespace std;
inline int read()
{
int s=0,w=1;
char ch=getchar();//读入
while(ch<'0'||ch>'9')//读掉其它字符
{
if(ch=='-')//w判断正负
{
w=-w;
ch=getchar();
}
}
while(ch>='0'&&ch<='9')
{
s*=10;//腾出个位
s+=(int)ch-'0';//个位加上 转的数字
ch=getchar();
}
return s*w;
}
int main()
{
int a;
a=read();
return 0;
}
加上时间测试:
快读程序:
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
clock_t a1,a2;
inline int read()
{
int s=0,w=1;
char ch=getchar();//读入
while(ch<'0'||ch>'9')//读掉其它字符
{
if(ch=='-')//w判断正负
{
w=-w;
ch=getchar();
}
}
while(ch>='0'&&ch<='9')
{
s*=10;//腾出个位
s+=(int)ch-'0';//个位加上 转的数字
ch=getchar();
}
return s*w;
}
int main()
{
freopen("a.txt","r",stdin);
int a;
a1=clock();
for(int i=0;i<100000;++i)
a=read();
a2=clock();
cout<<(double)(a2-a1)/CLOCKS_PER_SEC;
return 0;
}
cin程序:
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
clock_t a1,a2;
inline int read()
{
int x;
cin>>x;
return x;
}
int main()
{
freopen("a.txt","r",stdin);
int a;
a1=clock();
for(int i=0;i<100000;++i)
a=read();
a2=clock();
cout<<(double)(a2-a1)/CLOCKS_PER_SEC;
return 0;
}
scanf程序:
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
clock_t a1,a2;
inline int read()
{
int x;
scanf("%d",&x);
return x;
}
int main()
{
freopen("a.txt","r",stdin);
int a;
a1=clock();
for(int i=0;i<100000;++i)
a=read();
a2=clock();
cout<<(double)(a2-a1)/CLOCKS_PER_SEC;
return 0;
}
a.txt内容:
1000000个2147483647
快读结果:
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0.035 | 0.034 | 0.038 | 0.036 | 0.035 |
scanf结果:
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0.072 | 0.072 | 0.071 | 0.072 | 0.073 |
cin结果
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0.126 | 0.14 | 0.124 | 0.124 | 0.125 |
结果显而易见