码迷,mamicode.com
首页 > 其他好文 > 详细

[是模板哦] 快速读入

时间:2018-08-13 18:04:13      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:get   .com   text   ora   getchar   函数   东方   安利   pre   

原理(大概是)

getchar()比scanf()和cin都要快

利用非常快的读入函数getchar(),一位一位将数字以字符形式读入,若读入的字符不为数字则结束,首先判断要读入的数字的正负,之后每读入一个字符就将字符转化为数字并*10然后加上它

 

代码

这里提供两种书写函数的方式

 1 #include<cstdio>
 2 using namespace std;
 3  
 4 inline int read1(){//
 5     int f=1,x=0;
 6     char s=getchar();
 7     while(s<0 || s>9){
 8         if(s==-)
 9             f=-1;
10         s=getchar();
11     }
12     while(s>=0 && s<=9){
13         x=x*10+s-0;
14         s=getchar();
15     }
16     return x*f;
17 }
18 
19 void read2(int &x){
20     int f=1;
21     x=0;
22     char s=getchar();
23     while(s<0 || s>9){
24         if(s==-)
25             f=-1;
26         s=getchar();
27     }
28     while(s>=0 && s<=9){
29         x=x*10+s-0;
30         s=getchar();
31     }
32     x*=f;
33 }
34 
35 int main(){
36     int t1,t2;
37     t1=read1();
38     read2(t2);
39     return 0;
40 }

 

我感觉还不错

 

友情链接:安利一只小姐姐的博客

 

[是模板哦] 快速读入

标签:get   .com   text   ora   getchar   函数   东方   安利   pre   

原文地址:https://www.cnblogs.com/tatarakogasa/p/9469594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!